gpt4 book ai didi

java - 打开 SQLite 数据库时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 05:16:46 25 4
gpt4 key购买 nike

我正在尝试打开 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)

最佳答案

据我所知,您无法将 getApplication() 类型转换为 QuizApplication

将您的代码更改为以下内容。

currentGame = ((QuizApplication) getApplication()).getCurrentGame();
if(currentGame!=null)
{
currentQ = currentGame.getNextQuestion();
}

此外,如果您解决了 NPE,那么您将在以下 block 中得到 ActivityNotFoundException。您必须在 Intent 中指定目标 Activity 或操作。

Intent i = new Intent();
startActivity(i);
finish();

关于java - 打开 SQLite 数据库时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11072358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com