0

I wrote the code as below for saving state of textview:

public void onSaveInstanceState(Bundle b){
        System.out.println("save");
         TextView tv1=(TextView)findViewById(R.id.text1);

        b.putString(TEXTVIEW_STATE_KEY, tv1.getText().toString());
        super.onSaveInstanceState(b);
    }

then i retrieve as follows inside onCreate

if(savedInstanceState!=null && savedInstanceState.containsKey(TEXTVIEW_STATE_KEY));
        {
            System.out.println("hello");
       text=savedInstanceState.getString(TEXTVIEW_STATE_KEY);

        tv.setText(text);
        }

TEXTVIEW_STATE_KEY is a String constant.But while starting application it is throwing nullpointer exception in line

text=savedInstanceState.getString(TEXTVIEW_STATE_KEY);

Anyone having idea? plz help.

Android Killer
  • 18,174
  • 13
  • 67
  • 90

2 Answers2

1

Its because you are storing NULL in your Bundle while saving the save of the TextView. TextView should have some value before storing its state. Like this,

public void onSaveInstanceState(Bundle b){
         super.onSaveInstanceState(b);       
         System.out.println("save");
         TextView tv1=(TextView)findViewById(R.id.text1);
         tv1.setText("saving the value");
         b.putString(TEXTVIEW_STATE_KEY, tv1.getText().toString());
    }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • thanks buddy.but i have already set text inside oncreate.there is a logic which i m ensuring textview has text value in it.No problem with that.Just it is not working and showing NP in line as i told in my question – Android Killer Dec 05 '11 at 12:28
  • one problem can be this super.onSaveInstanceState(b); you are calling this at last which should be at top of the method. – Lalit Poptani Dec 05 '11 at 12:31
0

Very old question, but the answer is pretty simple...There is a semicolon (;) at the end of your if condition.

Rohan
  • 1,180
  • 2
  • 15
  • 28