我正在尝试通过使用来测试SharedPreferencesdeveloper.android.com SharedPreferences 。我试图稍微改变它,因为他们在onstop “可能永远不会被称为“。问题是,即使覆盖的第一行是super.onpause(),即使over.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(创建),因为我第一次启动应用程序也称为onpause()。

有帮助吗?

解决方案

protected void onPause() {}{ 
.

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

其他提示

看起来像一个简单的错字......请在oppause()之后,请注意空括号。可能被检测为您的方法。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top