Android App无法在模拟器中运行

时间:2015-09-21 20:18:08

标签: java android

我无法在模拟器上运行此应用程序。它是一个显示计数的简单应用程序,可以使用按钮增加或减少。它正确安装并打开正常,但只要按下按钮就会关闭异常显示"不幸的是App已停止工作"。我也附加了我的模拟器,环境细节和应用程序代码。

启动Point.java类

package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class StartingPoint extends Activity {

    int total;
    Button add,minus;
    TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting_point);


        add=(Button)findViewById(R.id.addBtn);
        minus=(Button)findViewById(R.id.delBtn);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                total=total+1;
                display.setText(" " +total);
            }
        });
        minus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                total=total-1;
                display.setText(" " +total);
            }
        });
    }
}

StartingPoint.xml类

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/DisplayTV"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Your total is : "
        android:textSize="45dp" />

    <Button
        android:id="@+id/addBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/deleteBtn"
        android:layout_marginTop="163dp"
        android:text="ADD" />

    <Button
        android:id="@+id/delBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/DisplayTV"
        android:layout_marginTop="179dp"
        android:text="Delete" />

</RelativeLayout>

应用启动后的控制台

[2015-09-22 01:27:14 - Test] ------------------------------
[2015-09-22 01:27:14 - Test] Android Launch!
[2015-09-22 01:27:14 - Test] adb is running normally.
[2015-09-22 01:27:14 - Test] Performing com.example.test.StartingPoint activity launch
[2015-09-22 01:27:19 - Test] Uploading Test.apk onto device 'emulator-5554'
[2015-09-22 01:27:21 - Test] Installing Test.apk...
[2015-09-22 01:27:37 - Test] Success!
[2015-09-22 01:27:37 - Test] Starting activity com.example.test.StartingPoint on device emulator-5554
[2015-09-22 01:27:39 - Test] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.test/.StartingPoint }

应用程序在此之后成功启动,但在应用程序意外关闭时显示错误。我也在下面给我的模拟器详细信息。

设备:Nexus 4 目标:Android 2.3.3 API lvl:10 内部存储:200 MiB

此外,LogCat上没有抛出任何错误,当我尝试使用智能手机(lollypop 5.0)运行应用程序时,响应相同。

请帮助,如果需要更多详细信息,请告诉我。感谢。

2 个答案:

答案 0 :(得分:-1)

您没有初始化 TextView ,请确保在setText()之前FindViewById。

最重要的是,我认为你没有初始化你的总数&#34; int变量。当你这样做

total = total + 1;

它为null,因此崩溃

尝试设置total = 0;紧跟在你的&#34; SetContentView&#34;

之后

答案 1 :(得分:-1)

这里使用此代码,这将有效。

public class StartingPoint extends Activity {

    int total=0;
    Button add,minus;
    TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting_point);
        display=(TextView)findViewById(R.id.DisplayTV);
        add=(Button)findViewById(R.id.addBtn);
        minus=(Button)findViewById(R.id.delBtn);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                total=total+1;
                display.setText(" " +total);
            }
        });
        minus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                total=total-1;
                display.setText(" " +total);
            }
        });
    }
}
相关问题