我正在尝试打开一个SQLite数据库,但是这个NullPointerException会继续弹出。
以下是java文件的代码:
public class QuestionActivity extends Activity implements OnClickListener{
private Question currentQ;
private GamePlay currentGame;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.question);
currentGame = ((QuizApplication) getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion();
Button nextBtn = (Button) findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(this);
setQuestions();
}
private void setQuestions() {
String question = Utility.capitalise(currentQ.getQuestion()) + "?";
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
List<String> answers = currentQ.getQuestionOptions();
TextView option1 = (TextView) findViewById(R.id.answer1);
option1.setText(Utility.capitalise(answers.get(0)));
TextView option2 = (TextView) findViewById(R.id.answer2);
option2.setText(Utility.capitalise(answers.get(1)));
TextView option3 = (TextView) findViewById(R.id.answer3);
option3.setText(Utility.capitalise(answers.get(2)));
TextView option4 = (TextView) findViewById(R.id.answer4);
option4.setText(Utility.capitalise(answers.get(3)));
}
public void onClick(View arg0) {
if (!checkAnswer()) return;
if (currentGame.isGameOver()){
Intent i = new Intent(this, QuizMenuActivity.class);
startActivity(i);
finish();
}
else{
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK :
return true;
}
return super.onKeyDown(keyCode, event);
}
private boolean checkAnswer() {
String answer = getSelectedAnswer();
if (answer==null){
return false;
}
else {
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
currentGame.incrementRightAnswers();
}
else{
currentGame.incrementWrongAnswers();
}
return true;
}
}
private String getSelectedAnswer() {
RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
if (c1.isChecked())
{
return c1.getText().toString();
}
if (c2.isChecked())
{
return c2.getText().toString();
}
if (c3.isChecked())
{
return c3.getText().toString();
}
if (c4.isChecked())
{
return c4.getText().toString();
}
return null;
}
}
以及LogCat中出现的错误阻止我继续:
06-17 14:40:22.845: E/AndroidRuntime(579): FATAL EXCEPTION: main
06-17 14:40:22.845: E/AndroidRuntime(579): java.lang.RuntimeException: Unable to start activity ComponentInfo{QuestionActivity}: java.lang.NullPointerException
06-17 14:40:22.845: E/AndroidRuntime(579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-17 14:40:22.845: E/AndroidRuntime(579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-17 14:40:22.845: E/AndroidRuntime(579): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-17 14:40:22.845: E/AndroidRuntime(579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-17 14:40:22.845: E/AndroidRuntime(579): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 14:40:22.845: E/AndroidRuntime(579): at android.os.Looper.loop(Looper.java:123)
06-17 14:40:22.845: E/AndroidRuntime(579): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-17 14:40:22.845: E/AndroidRuntime(579): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 14:40:22.845: E/AndroidRuntime(579): at java.lang.reflect.Method.invoke(Method.java:507)
06-17 14:40:22.845: E/AndroidRuntime(579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-17 14:40:22.845: E/AndroidRuntime(579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-17 14:40:22.845: E/AndroidRuntime(579): at dalvik.system.NativeStart.main(Native Method)
06-17 14:40:22.845: E/AndroidRuntime(579): Caused by: java.lang.NullPointerException
06-17 14:40:22.845: E/AndroidRuntime(579): at ....QuestionActivity.onCreate(QuestionActivity.java:38)
06-17 14:40:22.845: E/AndroidRuntime(579): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-17 14:40:22.845: E/AndroidRuntime(579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
答案 0 :(得分:0)
AFAIK你不能将getApplication()强制转换为QuizApplication
将您的代码更改为以下内容。
currentGame = ((QuizApplication) getApplication()).getCurrentGame();
if(currentGame!=null)
{
currentQ = currentGame.getNextQuestion();
}
此外,如果您解决NPE,那么您将在以下区块中获得ActivityNotFoundException
。您必须在意图中指定目标活动或行动。
Intent i = new Intent();
startActivity(i);
finish();