我无法在模拟器上运行这个应用程序。这是一个简单的应用程序,以显示计数,这可以增加或减少使用按钮。它安装正确,打开良好,但一旦按下按钮,它就会异常关闭,显示“不幸应用程序停止工作”。我正在附加我的模拟器,环境细节和应用程序代码。
启动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目标:Android2.3.3API lvl: 10内部存储: 200 MiB
另外,在LogCat上没有抛出错误,当我尝试使用智能手机运行应用程序时,响应是相同的(棒棒糖5.0)。
请帮助,如果需要更多的细节,请告诉我。谢谢。
发布于 2015-09-21 20:22:50
您没有初始化您的TextView,请确保在setText()之前也要对其进行FindViewById。
更重要的是,我认为你没有初始化你的“总”int变量。当你做的时候
total = total + 1;它是空的,所以它崩溃了
试着在"SetContentView“之后设置total = 0;
https://stackoverflow.com/questions/32703860
复制相似问题