I wrote the App that count something by press on + button and show it in text view. know I want to save the value of text view in onstop() method and use it in onstart and show that value on text view again.
Actually I did it but, when I click on + button the text view reset to 1 instead of increasing from last value. I actually save the text view value in onSaveInstanceState and use it on onRestoreInstanceState.
main.java code:
public class main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
int salavatcount ;
public String fonts="NAZANIN.TTF";
TextView salavatcounter;
Button addsalavat,sefrkon;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
salavatcount = 0;
}
setContentView(R.layout.main);
salavatcounter = (TextView) findViewById(R.id.salavatcounter);
addsalavat = (Button) findViewById(R.id.addsalavat);
addsalavat.setOnClickListener(this);
sefrkon = (Button) findViewById(R.id.sefrkon);
sefrkon.setOnClickListener(this);
SharedPreferences setting =getSharedPreferences("setting",0);
salavatcounter.setText(setting.getString("salavatcount", ""+0));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menu_option_infalter = getMenuInflater();
menu_option_infalter.inflate(R.menu.optionmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== R.id.about) {
Intent intent = new Intent(main.this,about.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("YourTextViewTextIdentifier",
salavatcounter.getText().toString()
);
savedInstanceState.putInt("int", salavatcount);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
salavatcounter.setText(savedInstanceState.getString("YourTextViewTextIdentifier"));
salavatcount = savedInstanceState.getInt("int");
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences setting=getSharedPreferences("setting", 0);
SharedPreferences.Editor editor=setting.edit();
editor.putString("salavatcount", salavatcounter.getText().toString());
editor.commit();
}
public void onClick(View v) {
if (v.getId()== R.id.addsalavat){
salavatcount++;
salavatcounter.setText(""+salavatcount);
}
else if (v.getId() == R.id.sefrkon) {
salavatcount=0;
salavatcounter.setText(""+salavatcount);
}
}
}
Edit:
log
04-24 01:59:50.823: W/dalvikvm(6645): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-24 01:59:50.833: E/AndroidRuntime(6645): Uncaught handler: thread main exiting due to uncaught exception
04-24 01:59:50.843: E/AndroidRuntime(6645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pishgamanit.salavatcounter/com.pishgamanit.salavatcounter.main}: java.lang.NullPointerException
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.os.Looper.loop(Looper.java:123)
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-24 01:59:50.843: E/AndroidRuntime(6645): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 01:59:50.843: E/AndroidRuntime(6645): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 01:59:50.843: E/AndroidRuntime(6645): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-24 01:59:50.843: E/AndroidRuntime(6645): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-24 01:59:50.843: E/AndroidRuntime(6645): at dalvik.system.NativeStart.main(Native Method)
04-24 01:59:50.843: E/AndroidRuntime(6645): Caused by: java.lang.NullPointerException
04-24 01:59:50.843: E/AndroidRuntime(6645): at com.pishgamanit.salavatcounter.main.onCreate(main.java:29)
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 01:59:50.843: E/AndroidRuntime(6645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-24 01:59:50.843: E/AndroidRuntime(6645): ... 11 more
04-24 01:59:50.863: I/dalvikvm(6645): threadid=7: reacting to signal 3
04-24 01:59:50.863: E/dalvikvm(6645): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
You are not setting the value of salavatcount to the value from you’re shared preference. When you click the button it is being reset to 0, as that is the default value of salavatcount.
You are also saving an int value as a string.
Then in your on create method, set the salavatcount value from the SharePreferences.
Edit:
I would rewrite the code like this: