1

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)
4

1 に答える 1

0

私の知る限り、 getApplication() を QuizApplication に型キャストすることはできません

コードを次のように変更します。

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

また、NPE を解決するとActivityNotFoundException、次のブロックに入ります。インテントでターゲット アクティビティまたはアクションを指定する必要があります。

Intent i = new Intent();
startActivity(i);
finish();
于 2012-06-17T15:29:39.660 に答える