为什么这一行崩溃我的应用程序?它甚至可以通过set clickable来实现。没有这条线,它很好,但有了它 - 它甚至不会进入活动。
continueButton.setEnabled(false);
这是logcat。
09-30 16:05:09.352: E/AndroidRuntime(755): FATAL EXCEPTION: main
09-30 16:05:09.352: E/AndroidRuntime(755): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mangodeveloper.mcathomie/com.mangodeveloper.mcathomie.McatActivityGame}: java.lang.NullPointerException
09-30 16:05:09.352: E/AndroidRuntime(755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-30 16:05:09.352: E/AndroidRuntime(755): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-30 16:05:09.352: E/AndroidRuntime(755): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-30 16:05:09.352: E/AndroidRuntime(755): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-30 16:05:09.352: E/AndroidRuntime(755): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 16:05:09.352: E/AndroidRuntime(755): at android.os.Looper.loop(Looper.java:123)
09-30 16:05:09.352: E/AndroidRuntime(755): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 16:05:09.352: E/AndroidRuntime(755): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 16:05:09.352: E/AndroidRuntime(755): at java.lang.reflect.Method.invoke(Method.java:507)
09-30 16:05:09.352: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 16:05:09.352: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 16:05:09.352: E/AndroidRuntime(755): at dalvik.system.NativeStart.main(Native Method)
09-30 16:05:09.352: E/AndroidRuntime(755): Caused by: java.lang.NullPointerException
09-30 16:05:09.352: E/AndroidRuntime(755): at com.mangodeveloper.mcathomie.McatActivityGame.onCreate(McatActivityGame.java:40)
09-30 16:05:09.352: E/AndroidRuntime(755): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-30 16:05:09.352: E/AndroidRuntime(755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-30 16:05:09.352: E/AndroidRuntime(755): ... 11 more
此处是截至目前的整个活动:
package com.mangodeveloper.mcathomie;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class McatActivityGame extends Activity {
private static int GAME_PREFERENCES_MAXROUNDS = 1, GAME_PREFERENCES_CURRENTROUND;
private Cursor c;
private RadioGroup radioGrp;
private Button continueButton, pauseButton, explainButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
radioGrp = (RadioGroup) findViewById(R.id.radioGroup1);
continueButton = (Button) findViewById(R.id.button_continue);
pauseButton = (Button) findViewById(R.id.button_pause);
explainButton = (Button)findViewById(R.id.button_explanation);
setContentView(R.layout.activity_game);
McatDatabase dbHelper = new McatDatabase(this);
c = dbHelper.getQuestions();
c.moveToFirst();
fillQuestions();
continueButton.setEnabled(false);
// radioGrp.setOnClickListener(new OnClickListener() {
//
// public void onClick(View v) {
// continueButton.setEnabled(true);
// }
// });
// continueButton.setOnClickListener(new OnClickListener() {
// public void onClick(View v) {
// int selectedId = radioGrp.getCheckedRadioButtonId();
// RadioButton radioBttnSelected = (RadioButton) findViewById(selectedId);
// if (radioBttnSelected.getText() == answer1) {
// Toast.makeText(McatActivityGame.this, "Correct Answer!", Toast.LENGTH_SHORT).show();
// }else{Toast.makeText(McatActivityGame.this, "Not correct, please try again", Toast.LENGTH_SHORT).show();
// }
// }
// });
// nextquestionButton.setOnClickListener(new OnClickListener() {
//
// public void onClick(View v) {
// GAME_PREFERENCES_CURRENTROUND++;
// GAME_PREFERENCES_MAXROUNDS = getIntent().getExtras().getInt("MAXROUNDS");
//
// if (GAME_PREFERENCES_CURRENTROUND > GAME_PREFERENCES_MAXROUNDS) {
// Toast.makeText(McatActivityGame.this, "Alert Dialog stuff", Toast.LENGTH_SHORT).show();
// }else{
// c.moveToNext();
// fillQuestions();
// }
// }
// });
//
}
private void fillQuestions() {
// shuffle questions
List<String> shuffAnswr = new ArrayList<String>(4);
shuffAnswr.add(c.getString(3));
shuffAnswr.add(c.getString(4));
shuffAnswr.add(c.getString(5));
shuffAnswr.add(c.getString(6));
Collections.shuffle(shuffAnswr);
// instantiate
TextView questionTv = (TextView) findViewById(R.id.textView0);
RadioButton rd0 = (RadioButton)findViewById(R.id.radio0),
rd1 = (RadioButton)findViewById(R.id.radio1),
rd2 = (RadioButton)findViewById(R.id.radio2),
rd3 = (RadioButton)findViewById(R.id.radio3);
// set questions
questionTv.setText(c.getString(2));
rd0.setText(shuffAnswr.get(0));
rd1.setText(shuffAnswr.get(1));
rd2.setText(shuffAnswr.get(2));
rd3.setText(shuffAnswr.get(3));
}
}
答案 0 :(得分:1)
将onCreate更改为以下内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);//This got moved up
radioGrp = (RadioGroup) findViewById(R.id.radioGroup1);
continueButton = (Button) findViewById(R.id.button_continue);
pauseButton = (Button) findViewById(R.id.button_pause);
explainButton = (Button)findViewById(R.id.button_explanation);
McatDatabase dbHelper = new McatDatabase(this);
c = dbHelper.getQuestions();
c.moveToFirst();
fillQuestions();
continueButton.setEnabled(false);
....... //Rest of your code
您收到错误是因为您在布局膨胀之前尝试在布局中获取对视图的引用。在使用setContentView()
之前,您必须始终致电findViewById()
。