我想通過使用developer.android.com SharedPreferences中的代碼來測試SharedPreferences。自從他們說onStop "may never be called"後,我試圖稍微改變它。問題是,即使覆蓋的第一行是super.onPause(),onPause也會給我這個奇怪的錯誤。onPause():SuperNotCalledException即使super.onPause()存在
03-09 14:41:00.883: E/AndroidRuntime(394): android.app.SuperNotCalledException: Activity {com.mobinoob.saveinfo/com.mobinoob.saveinfo.SaveInfoActivity} did not call through to super.onPause()
下面是代碼示例:
public class SaveInfoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListener();
loadText();
textSaved = "";
created = true;
}
private boolean created = false;
private void addListener() {
EditText et = (EditText) findViewById(R.id.editText1);
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
textSaved = s.toString();
System.out.println("text:" + textSaved);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
}
private static String fileName = "savedInfo.txt";
private static String propertyName = "text";
private String textSaved;
@Override
protected void onPause() {}{
super.onPause();
if (created) {
saveText();
}
}
private void loadText() {
SharedPreferences settings = getSharedPreferences(fileName, MODE_PRIVATE);
if (settings == null)
return;
String result = settings.getString(propertyName, "");
setText(result);
}
private void saveText() {
System.out.println("saving");
SharedPreferences settings = getSharedPreferences(fileName, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(propertyName, textSaved);
editor.commit();
}
private void setText(String result) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(result);
System.out.println("restore" + result);
}
任何想法,我失去了一些東西?還要注意,因爲onPause()也是我第一次啓動應用程序,所以需要if(創建)。
10分鐘註冊併發布問題和什麼? 10秒,發現一個愚蠢的錯字。:)我讀了幾次,沒有看到。謝謝! – maugch 2012-03-09 14:16:03