我正在尝试使用developer.android.com SharedPreferences中的代码测试SharedPreferences。由于他们说onStop "may never be called",因此我尝试对其进行了一些更改。问题是即使重写的第一行是super.onPause(),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);
}
知道我在想什么吗?还要注意所需的if(created),因为在我第一次启动应用程序时也会调用onPause()。
最佳答案
protected void onPause() {}{
您那里还有一对大括号,使其看起来像一个空函数。
关于android - onPause():即使存在super.onPause(),SuperNotCalledException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9635372/