我一直试图让这个工作很长一段时间,我已经检查了我可以找到的每个问题,但是这并没有给我任何错误,我没有太多工作要做!基本上,我有一个按钮,它将一个int添加到一个int中,该int将显示在它旁边的TextView中。我希望int在应用程序关闭时保留,所以它看起来像sharedpreferences是我最好的选择(我也考虑过sqlite,但我不确定它是否可以在不更新应用程序的情况下进行编辑,而且这看起来更简单)。SharedPreferences的问题
所以,我的问题是,当我按下后退按钮/关闭应用程序等
这里的,这是所有在EDIT发生的活动int获取增加了罚款,但重置为0:这是我的到目前为止的代码(有更多的其他IFS)
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ThirdScreenActivity extends Activity {
public int intPlusOne;
public int intPlusOne2;
public static final String PREFS = "MyPrefsFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
//Sets the title to recipe name
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(position);
//Sets the correct recipe
if ("ListView Item 1".equals(position)) {
TextView t1 = ((TextView)findViewById(R.id.TextView1));
t1.setText(R.string.string1, null);
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
PlusOneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intPlusOne++;
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
}
});
}
else if ("ListView Item 2".equals(position)) {
TextView t1= ((TextView)findViewById(R.id.TextView1));
t1.setText(R.string.string2, null);
SharedPreferences sharedPrefs = this.getSharedPreferences("PREFS", MODE_PRIVATE);
intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0);
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne2));
Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
PlusOneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intPlusOne2++;
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne2));
}
});
}
public void onPause() {
super.onPause();
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putInt("intPlusOneSaved", intPlusOne);
editor.commit(); //also tried removing this line
editor.putInt("intPlusOne2Saved", IntPlusOne2);
editor.commit();
}
}
我很乐意提供任何更多的细节,如果有帮助!
非常感谢,你已经解决了!我不确定这是否是onDestroy/onPause修复或其他问题,但我花了很多时间在此,非常感谢。 – stackUnderflow
您未在'onCreate'方法中初始化您的'intPlusOne'属性。 – Brtle
嗯,我还有一个小问题,但我不确定是否要问另一个问题,或者它是否可以快速解决。我有不止一个“如果x等于位置”(使用else if),并且它们各自独立工作,即使应用程序已关闭,但如果我+1 +1项,请返回+1另一项,第一个重置为0!你能帮忙吗? (看编辑的问题) – stackUnderflow