Reputation: 1318
I'm trying to test SharedPreferences by using the code in developer.android.com SharedPreferences. I tried to change it slightly since they say onStop "may never be called". The problem is that onPause gives me this weird error even if the first line of the override is a 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()
Here is an example of the code:
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);
}
Any idea where I'm missing something? Also note the if(created) that is needed because onPause() is also called the first time I start the application.
Upvotes: 1
Views: 3271
Reputation: 22158
Looks like a simple typo to me...notice the empty braces right after onPause(). That is likely being detected as your method.
@Override
protected void onPause() {}{
super.onPause();
if (created) {
saveText();
}
}
Upvotes: 1
Reputation: 2196
protected void onPause() {}{
You've got an extra pair of curly braces in there, making it look like an empty function.
Upvotes: 5