我编写了通过按 + 按钮计算某些内容的应用程序,并将其显示在文本视图中。知道我想在 onstop() 方法中保存文本视图的值并在 onstart 中使用它并再次在文本视图上显示该值。实际上我做到了,但是当我单击 + 按钮时,文本视图重置为 1 而不是从最后一个值增加。我实际上将文本视图值保存在 onSaveInstanceState 中并在 onRestoreInstanceState 上使用它。main.java 代码:
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);
}
}
}
编辑:日志
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