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

时间:2012-03-09 14:08:26

标签: android sharedpreferences onpause

我正在尝试使用developer.android.com SharedPreferences中的代码来测试SharedPreferences。我试着改变它,因为他们说onStop "may never be called"。问题是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()

以下是代码示例:

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()也是在我第一次启动应用程序时调用的。

2 个答案:

答案 0 :(得分:5)

protected void onPause() {}{ 

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

答案 1 :(得分:1)

看起来像是一个简单的拼写错误...注意onPause()之后的空括号。这可能被检测为您的方法。

@Override 
protected void onPause() {}{
     super.onPause();
     if (created) {
         saveText();
     }
}