2012-10-27 42 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

是的,我做到了。只是没有把它放在描述中。编辑 –