2012-10-27 31 views
1

我做這個小的基於文本的RPG遊戲,用戶可以選擇自己的名字,種族,階級,等...的onResume()從的onPause()

我有一個活動不拉的信息在用戶類型他們想要使用的名字,然後從旋轉器中選擇比賽類。

然後,當他們點擊創建字符按鈕時,它會轉移到下一個活動。這是完美的。

問題出在onPause事件保存數據時。 onResume()方法不起作用?意見只是顯示空白。

赫雷什所選擇的代碼:

(這是其中數據是從其他活動拉動)

String Name = getIntent().getStringExtra("strName"); 
    textViewStrName.setText(Name); 

    String Race = getIntent().getStringExtra("strRace"); 
    textViewStrRace.setText(Race); 

    String Class = getIntent().getStringExtra("strClass"); 
    textViewStrClass.setText(Class); 

(這是的onPause()方法)

SharedPreferences preferences = getPreferences(MODE_PRIVATE); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.clear(); 
    String strName = textViewStrName.getText().toString(); 
    String strRace = textViewStrRace.getText().toString(); 
    String strClass = textViewStrClass.getText().toString(); 
    editor.putString("strName", strName); 
    editor.putString("strRace", strRace); 
    editor.putString("strClass", strClass); 
editor.commit(); 

(這是onResume()方法)

SharedPreferences preferences = getPreferences(MODE_PRIVATE); 
    TextView textViewStrName = (TextView) findViewById(R.id.textViewStrName); 
    TextView textViewStrRace = (TextView) findViewById(R.id.textViewStrRace); 
    TextView textViewStrClass = (TextView) findViewById(R.id.TextViewStrClass); 
    TextView textViewStrAlliance = (TextView) findViewById(R.id.textViewStrAlliance); 


     textViewStrName.setText(preferences.getString("strName", null)); 
     textViewStrRace.setText(preferences.getString("strRace", null)); 
     textViewStrClass.setText(preferences.getString("strClass", null)); 
     textViewStrAlliance.setText(preferences.getString("strAlliance", 
       null)); 

我在運行應用程序時沒有收到任何錯誤。

+1

定義「只是不行」 –

+0

嘗試在最後調用'super.onResume();'。 –

+0

就好像它甚至沒有調用方法 –

回答

0

如果要在活動之間使用SharedPreferences存儲,請使用getSharedPreferences()而不是getPreferences。看詳情here

也請不要忘記在進行更改後致電editor.commit()

+0

是的,我做到了。只是沒有把它放在描述中。編輯 –