0

当我在 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);
    }

}

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

4

2 回答 2

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的。

于 2013-01-04T18:49:57.883 回答
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];

如果它只是一个静态数组。因为根据您的代码,您的复选框数组将包含 4 个元素。希望能帮助到你。

于 2013-01-07T11:05:36.290 回答