Java 打开SQLite数据库时出现NullPointerException

Java 打开SQLite数据库时出现NullPointerException,java,android,sqlite,nullpointerexception,Java,Android,Sqlite,Nullpointerexception,我正试图打开一个SQLite数据库,但这个NullPointerException一直在弹出 以下是java文件的代码: public class QuestionActivity extends Activity implements OnClickListener{ private Question currentQ; private GamePlay currentGame; @Override public void onCreate(Bundle savedInstan

我正试图打开一个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;
    }


  }

如果您不能将getApplication()类型转换为QuizaApplication

将代码更改为以下内容

currentGame = ((QuizApplication) getApplication()).getCurrentGame();
if(currentGame!=null)
{
  currentQ = currentGame.getNextQuestion();
}
此外,如果您解决NPE,则将在下面的块中获得
ActivityNotFoundException
。您必须在intent中指定目标活动或操作

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

一个粗略的猜测是,您的按钮找不到(使用
findViewById()
,因此当您尝试添加
OnClickListener
时,会抛出NPE。但是您应该自己检查第38行……这一行:
位于……QuestionActivity.onCreate(QuestionActivity.java:38)
告诉我错误发生在代码的第38行,但由于导入声明已被删除,我们无法真正确定该行。您应该检查该行,如果您仍然有问题,请编辑您的问题以调用该行或包含导入。我怀疑@LukasKnuth是对的;要么是该行,要么是currentGame为null。这是一个与您的问题无关的小更正。当您在主类中实现
OnClickListener
时,为什么要为
nextBtn.setOnClickListener()
创建一个匿名内部类?您可以在主类的
onClick(视图arg0)
中编写该代码。我对currentGame有疑问=((QuizaApplication)getApplication()).getCurrentGame();能否检查currentGame是否为null?错误38的行是-currentQ=currentGame.getNextQuestion();但是az Vipul Shah说这可能是上一个,但是我如何检查它是否为null?我没有在任何地方声明它为null?Chearshaha,现在我在NPE上也有问题:setQuestions();和:String question=Utility.capitalize(currentQ.getQuestion())+“?”;有什么想法吗?你给我的代码实际上是currentQ函数正确吗?比x好很多
Intent i = new Intent();
startActivity(i);
finish();