首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onPause():即使super.onPause()存在,也执行SuperNotCalledException

onPause():即使super.onPause()存在,也执行SuperNotCalledException
EN

Stack Overflow用户
提问于 2012-03-09 22:08:26
回答 2查看 3.2K关注 0票数 1

我正在尝试使用developer.android.com SharedPreferences中的代码来测试SharedPreferences。我试着稍微改一下,因为他们说的是onStop "may never be called"。问题是,即使覆盖的第一行是super.onPause(),onPause也会给我这个奇怪的错误。

代码语言:javascript
复制
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()

以下是代码的示例:

代码语言:javascript
复制
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()。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-09 22:11:17

代码语言:javascript
复制
protected void onPause() {}{ 

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

票数 5
EN

Stack Overflow用户

发布于 2012-03-09 22:12:05

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

代码语言:javascript
复制
@Override 
protected void onPause() {}{
     super.onPause();
     if (created) {
         saveText();
     }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9635372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档