2014-07-11 36 views
0

我的問題是,TextView給java.lang.NullPointerException如果我做方向更改頻繁。我在下面粘貼了我的代碼。我錯過了什麼嗎?頻繁的方向更改爲Textview提供空指針例外

@Override 
public void onSaveInstanceState(Bundle outState) { 

    if (title.getText().toString() != null) {  //Getting Null Exception here 
     Log.i("onSaveInstanceState", " "+title.getText().toString()); 
     outState.putString("title", title.getText().toString()); 
    } 
} 

-

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    view=inflater.inflate(R.layout.add, null); 
    title =(TextView) view.findViewById(R.id.title); 

    if (savedInstanceState.getString("title") != null) { 
     Log.i("on create", " "+savedInstanceState.getString("title")); 
     title.setText(savedInstanceState.getString("title")); 
    } 
} 

回答

0

嘗試這種方式

@Override 
public void onSaveInstanceState(Bundle outState) { 

    if (title.getText() != null) {  //Getting Null Exception here 
     Log.i("onSaveInstanceState", " "+title.getText().toString()); 
     outState.putString("title", title.getText().toString()); 
    } 
} 

你進行比較,評價東西是空的結果,這樣,它會給你一個NullPointerException。

UPDATE

Reffer這個SO帖子裏接受的答案指出,你需要再次的onSaveInstanceState中的方法TextView的插件搜索。

how to save state with onSaveInstanceState and onRestoreInstanceState while orientation change

+0

感謝烏拉圭回合的答覆, 「title.getText()」 本身給空指針異常,任何想法? – kavie

+0

你有沒有在你的onCreate()方法之外聲明'標題'? – axierjhtjz

+0

是啊............ – kavie