Android应用在发布时停止工作

时间:2013-01-04 18:48:07

标签: java android

我在AVD中启动它时,我的应用程序停止工作。给我“不幸的是{app name}已停止工作。”

这是运行时的日志:

01-07 10:52:06.162: E/AndroidRuntime(771): FATAL EXCEPTION: main
01-07 10:52:06.162: E/AndroidRuntime(771): java.lang.IllegalStateException: Could not     execute method of the activity
01-07 10:52:06.162: E/AndroidRuntime(771):  at  android.view.View$1.onClick(View.java:3597)
01-07 10:52:06.162: E/AndroidRuntime(771):  at android.view.View.performClick(View.java:4202)
01-07 10:52:06.162: E/AndroidRuntime(771):  at android.view.View$PerformClick.run(View.java:17340)
01-07 10:52:06.162: E/AndroidRuntime(771):  at android.os.Handler.handleCallback(Handler.java:725)
01-07 10:52:06.162: E/AndroidRuntime(771):  at  android.os.Handler.dispatchMessage(Handler.java:92)
01-07 10:52:06.162: E/AndroidRuntime(771):  at android.os.Looper.loop(Looper.java:137)
01-07 10:52:06.162: E/AndroidRuntime(771):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-07 10:52:06.162: E/AndroidRuntime(771):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:52:06.162: E/AndroidRuntime(771):  at java.lang.reflect.Method.invoke(Method.java:511)
01-07 10:52:06.162: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-07 10:52:06.162: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-07 10:52:06.162: E/AndroidRuntime(771):  at dalvik.system.NativeStart.main(Native Method)
01-07 10:52:06.162: E/AndroidRuntime(771): Caused by: java.lang.reflect.InvocationTargetException
01-07 10:52:06.162: E/AndroidRuntime(771):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:52:06.162: E/AndroidRuntime(771):  at java.lang.reflect.Method.invoke(Method.java:511)
01-07 10:52:06.162: E/AndroidRuntime(771):  at android.view.View$1.onClick(View.java:3592)
01-07 10:52:06.162: E/AndroidRuntime(771):  ... 11 more
01-07 10:52:06.162: E/AndroidRuntime(771): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
01-07 10:52:06.162: E/AndroidRuntime(771):  at com.ultimanaire.MainActivity.playGame(MainActivity.java:38)
01-07 10:52:06.162: E/AndroidRuntime(771):  ... 14 more

以下是我第一次发布问题时忘记添加的MainActivity.java代码。有人说第18行,即:

if (sub.isChecked()) {

以下是MainActivity.java的代码:

package com.ultimanaire;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;


public class MainActivity extends Activity {

    String[] subjects = {};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void playGame(View view) {
        int r = 0;
       CheckBox t = (CheckBox)findViewById(R.id.subject_math);
        CheckBox y = (CheckBox)findViewById(R.id.subject_science);
        CheckBox u = (CheckBox)findViewById(R.id.subject_history);
        CheckBox i = (CheckBox)findViewById(R.id.subject_foreign_language);
        CheckBox[] stored_subjects =  {t, y, u, i};
        for(CheckBox sub : stored_subjects) {

            if (sub.isChecked()) {
                 subjects[r] = (String) sub.getText();
            r = r +1;
            }

        }

        Intent intent = new Intent(this, GameActivity.class);
        intent.putExtra("SUBJECT", subjects);
        startActivity(intent);
    }

}

所以,我已经弄清楚为什么nullpointer发生并修复它,然后它带来另一个,并设法修复它,但现在它又出现了另一个我无法弄清楚的。我编写了代码,以便我现在修复它,以及提供nullpointer的行。日志猫也会更新。设备上的相同消息会显示“不幸的是,appname已经停止工作,但是在打开应用程序时却没有,现在它打开了它,但是在我使用playGame方法作为onClick的播放按钮时,它会这样做。

2 个答案:

答案 0 :(得分:4)

17:56:19.114: E/AndroidRuntime(901): Caused by: java.lang.NullPointerException 01-04 17:56:19.114: E/AndroidRuntime(901): at com.ultimanaire.MainActivity.(MainActivity.java:18) 01-04 17:56:19.114: 

基于堆栈跟踪,归因于NullPointerException中第18行的MainActivity.java

没有问题代码发布,因此我们无法预测导致此NullpointerException的原因。

答案 1 :(得分:0)

您正在获取数组索引超出范围的异常,因为您正在将其初始化为

String[] subjects = {};

所以它的长度为0.你试图使用

访问它
subjects[r] = (String) sub.getText();

哪个抛出异常。 有两种选择:

1> 将其初始化为

String[] subjects = null;

然后添加

subjects = new String[stored_subjects.length];

这将提供动态String []。大小取决于复选框数组的长度。

2 - ; 将其初始化为

String[] subjects = new String[4];

如果它只是一个静态数组。因为您的checkbox数组将根据您的代码包含4个元素。 希望它有所帮助。