0

My application is working fine except when i rotate device, in that case it gives NullPointerException.To handle screen rotation i have already overridden onSaveInstanceState.i checked logcat also it says Caused by: java.lang.NullPointerException 10-28 23:51:20.769: E/AndroidRuntime(25185): at com.bignerdranch.android.geoquiz.CheatActivity.onCreate(CheatActivity.java:42) code at line 42 is mAnswerTextView.setText(R.string.false_button). java file as well as logcat detail are posted below.What could be the reason ? i am new to android.

java:

    public class CheatActivity extends Activity {

        public static final String EXTRA_ANSWER_IS_TRUE =
                "com.bignerdranch.android.geoquiz.answer_is_true";
        private boolean mAnswerIsTrue;
        private TextView mAnswerTextView;
        private Button mShowAnswer;
        public static final String EXTRA_ANSWER_SHOWN="om.bignerdranch.android.geoquiz.answer_shown";
        public static final String KEY_CHEATER = "cheater";
        private boolean mAnswerShown; 

        private void setAnswerShowResult(boolean isAnswerShown){
            Intent data= new Intent();
            data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
            setResult(RESULT_OK,data);
            mAnswerShown = isAnswerShown;
        }
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activiy_cheat);
             if (savedInstanceState == null) {
                    setAnswerShowResult(false);
                }
             if (savedInstanceState != null){
                 setAnswerShowResult(savedInstanceState.getBoolean(KEY_CHEATER, false));
                 if(mAnswerIsTrue){
                    mAnswerTextView.setText(R.string.true_button);
                 } else{
                    mAnswerTextView.setText(R.string.false_button);
                 }
              }
            mAnswerIsTrue = getIntent()
                    .getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
            mAnswerTextView= (TextView) findViewById(R.id.answerTextView);
            mShowAnswer=(Button) findViewById(R.id.showAnswerButton);
            //setAnswerShowResult(false);
            mShowAnswer.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(mAnswerIsTrue){
                        mAnswerTextView.setText(R.string.true_button);
                    }else{
                        mAnswerTextView.setText(R.string.false_button);
                    }
                    setAnswerShowResult(true);
                }
            });

        }

         public void onSaveInstanceState(Bundle savedInstanceState){

super.onSaveInstanceState(savedInstanceState);           
savedInstanceState.putBoolean(KEY_CHEATER, mAnswerShown);

           }

    }

LogCat:

10-28 23:51:20.769: E/AndroidRuntime(25185): FATAL EXCEPTION: main
10-28 23:51:20.769: E/AndroidRuntime(25185): Process: com.bignerdranch.android.geoquiz, PID: 25185
10-28 23:51:20.769: E/AndroidRuntime(25185): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.CheatActivity}: java.lang.NullPointerException
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3805)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.ActivityThread.access$900(ActivityThread.java:139)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.os.Looper.loop(Looper.java:136)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.ActivityThread.main(ActivityThread.java:5086)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at java.lang.reflect.Method.invokeNative(Native Method)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at java.lang.reflect.Method.invoke(Method.java:515)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at dalvik.system.NativeStart.main(Native Method)
10-28 23:51:20.769: E/AndroidRuntime(25185): Caused by: java.lang.NullPointerException
10-28 23:51:20.769: E/AndroidRuntime(25185):    at com.bignerdranch.android.geoquiz.CheatActivity.onCreate(CheatActivity.java:42)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.Activity.performCreate(Activity.java:5248)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
10-28 23:51:20.769: E/AndroidRuntime(25185):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
10-28 23:51:20.769: E/AndroidRuntime(25185):    ... 12 more
hemkar
  • 27
  • 1
  • 6

2 Answers2

2

You are calling setText() on mAnswerTextView before you assign a value to mAnswerTextView. Move your savedInstanceState work to the end of onCreate(), after you have called findViewById() to retrieve your widgets.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

On line 42, you are trying call setText on mAnswerTextView, but it has not been initialized. (Because when you change the orientation of the device, onCreate is called again).

To fix this, you simply need to initialize the TextView before you call setText. Move the following line

mAnswerTextView= (TextView) findViewById(R.id.answerTextView);

above where you check if savedInstanceState is null (as long as it's still after setContentView, calling it before this would cause another error)

if (savedInstanceState != null){
....

This will ensure that when the activity is first created, and whenever it's recreated, you will be able to call setText on your TextView

Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
  • thanks man.My bad.it is working fine now. But why i was not getting error for the first time when activity was created ? why only after rotation i got nullpointerException ? – hemkar Oct 28 '15 at 18:53
  • Because the first time, `savedInstanceState` is not null, so the code inside there doesn't get called. The other instances in `onCreate` where you call `setText` are after you initialize the `TextView`, so those work fine. – Andrew Brooke Oct 28 '15 at 21:38