onPause():即使super.onPause()存在,SuperNotCalledException也是如此

mau*_*gch 1 android sharedpreferences onpause

我正在尝试使用developer.android.com SharedPreferences中的代码来测试 SharedPreferences.我试图稍微改变它,因为他们说onStop "可能永远不会被称为".问题是onPause给了我这个奇怪的错误,即使覆盖的第一行是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()
Run Code Online (Sandbox Code Playgroud)

以下是代码示例:

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);
}
Run Code Online (Sandbox Code Playgroud)

知道我错过了什么吗?还要注意所需的if(created),因为onPause()也是在我第一次启动应用程序时调用的.

小智 5

protected void onPause() {}{ 
Run Code Online (Sandbox Code Playgroud)

你在那里有一对额外的花括号,使它看起来像一个空函数.