Question

Here is the code. Not sure if any more information is needed. The app works exactly as expected and desired except on screen rotation. When the screen is rotated this crash occurs. I am new to Android and trying to learn so please let me know if there is anything else needed.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_quiz);

    if(savedInstanceState != null){
        mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
    }

    mCheatButton = (Button)findViewById(R.id.cheat_button);
    mCheatButton.setOnClickListener(new View.OnClickListener() {//this is line 127

        @Override
        public void onClick(View v) {
            Intent i = new Intent(QuizActivity.this, CheatActivity.class);
            boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
            i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
            startActivityForResult(i, 0);
        }
    });

here is the logcat:

03-18 06:54:35.203: E/AndroidRuntime(5411): FATAL EXCEPTION: main
03-18 06:54:35.203: E/AndroidRuntime(5411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geoquiz/com.example.geoquiz.QuizActivity}: java.lang.NullPointerException
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.os.Looper.loop(Looper.java:137)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at java.lang.reflect.Method.invoke(Method.java:511)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at dalvik.system.NativeStart.main(Native Method)
03-18 06:54:35.203: E/AndroidRuntime(5411): Caused by: java.lang.NullPointerException
03-18 06:54:35.203: E/AndroidRuntime(5411):     at com.example.geoquiz.QuizActivity.onCreate(QuizActivity.java:127)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.Activity.performCreate(Activity.java:5104)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-18 06:54:35.203: E/AndroidRuntime(5411):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-18 06:54:35.203: E/AndroidRuntime(5411):     ... 12 more
Was it helpful?

Solution

I guess you have a alternative layout for landscape and portrait. And one of the layouts does not have a button with id cheat_button.

If you say the NPE is @ mCheatButton.setOnClickListener(new View.OnClickListener() then mCheatButton us null.

Make sure the layouts have a button with id cheat_button

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top