当我在 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 的播放按钮计时时它会这样做。