我正在尝试创建一个问答游戏,但是当我点击回复按钮时,我会得到一个NullPointerException
。
似乎所有变量都已初始化,任何人都可以帮助我吗?
这是班级:
public class QActivity extends Activity {
protected int qst = 0;
protected int err = 0;
protected String mode = "";
protected Intent intent;
protected ArrayList<Integer> tb;
protected Integer x = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_q);
Button r1 = (Button) findViewById(R.id.rep1);
Button r2 = (Button) findViewById(R.id.rep2);
Button r3 = (Button) findViewById(R.id.rep3);
TextView txt = (TextView) findViewById(R.id.questiontxt);
Question q = new Question();
DatabaseHelper myDbHelper = new DatabaseHelper(this);
tb = getIntent().getExtras().getIntegerArrayList("tb");
mode = getIntent().getExtras().getString("Mode");
qst = getIntent().getExtras().getInt("qst");
err = getIntent().getExtras().getInt("err");
if (mode.equals("25")) {
x = 0;
do {
x = (int) (Math.random() * 57);
} while (Verif(x, tb) == true);
tb.add(x);
qst += 1;
try {
q = myDbHelper.getQuestion(x);
} catch (SQLException sqle) {
throw sqle;
}
txt.setText(q.getQuestion());
r1.setText(q.getAnswer1());
r2.setText(q.getAnswer2());
r3.setText(q.getAnswer3());
TextView t1 = (TextView) findViewById(R.id.questid);
TextView t3 = (TextView) findViewById(R.id.questnumber);
intent = new Intent(this, QActivity.class);
intent.putExtra("Mode", mode);
intent.putExtra("qst", qst);
intent.putExtra("err", err);
intent.putExtra("tab", tb);
}
r1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(intent);
}
});
}
public boolean Verif(int n, ArrayList<Integer> tbl) {
for (Integer s : tbl) {
if (s == n)
return true;
}
return false;
}
@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_q, menu);
return true;
}
}
这是LogCat输出:
12-12 00:27:47.233: W/dalvikvm(7142): threadid=1: thread exiting with uncaught exception (group=0x40c471f8)
12-12 00:27:47.233: E/AndroidRuntime(7142): FATAL EXCEPTION: main
12-12 00:27:47.233: E/AndroidRuntime(7142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dreamappz.alamalmaarefa/com.dreamappz.alamalmaarefa.QActivity}: java.lang.NullPointerException
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.os.Looper.loop(Looper.java:137)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.app.ActivityThread.main(ActivityThread.java:4511)
12-12 00:27:47.233: E/AndroidRuntime(7142): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 00:27:47.233: E/AndroidRuntime(7142): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 00:27:47.233: E/AndroidRuntime(7142): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
12-12 00:27:47.233: E/AndroidRuntime(7142): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
12-12 00:27:47.233: E/AndroidRuntime(7142): at dalvik.system.NativeStart.main(Native Method)
12-12 00:27:47.233: E/AndroidRuntime(7142): Caused by: java.lang.NullPointerException
12-12 00:27:47.233: E/AndroidRuntime(7142): at com.dreamappz.alamalmaarefa.QActivity.Verif(QActivity.java:87)
12-12 00:27:47.233: E/AndroidRuntime(7142): at com.dreamappz.alamalmaarefa.QActivity.onCreate(QActivity.java:47)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.app.Activity.performCreate(Activity.java:4470)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-12 00:27:47.233: E/AndroidRuntime(7142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
12-12 00:27:47.233: E/AndroidRuntime(7142): ... 11 more
答案 0 :(得分:2)
您应该更改此行:
intent.putExtra("tab", tb);
到此:
intent.putExtra("tb", tb);
答案 1 :(得分:0)
我建议使用此clickListener
r1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (intent!=null)
startActivity(intent);
}
});
因为,如果你的mode.equals(“25”)为false,那么你的意图为空。