Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用共享首选项仅保存1 int_Java_Android - Fatal编程技术网

Java 使用共享首选项仅保存1 int

Java 使用共享首选项仅保存1 int,java,android,Java,Android,我正在创建一个android应用程序,其中我有2个静态INT。我想保存这两个int的值,但目前只保存1个int 以下是我目前保存ints的代码: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // The activity is being created. setContentView(R.layout.durood); app_preferen

我正在创建一个android应用程序,其中我有2个静态INT。我想保存这两个int的值,但目前只保存1个int

以下是我目前保存ints的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.durood);


app_preferences = this.getSharedPreferences("myPrefscount", MODE_WORLD_READABLE);


count = app_preferences.getInt("count", 0);
total = app_preferences.getInt("total", 0);

 @Override
protected void onPause() {
super.onPause();

 SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.putInt("total", total);
editor.commit();
editor.commit();
正在保存“count”int,但不是“total”int。关于如何更正此问题,有什么想法吗

在twaddington的建议之后编辑了最后一段代码:

SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.putInt("total", total);
Log.d("Test", "total: "+total);
editor.commit();
我的全部代码:

public class durood extends Activity{

//Count Button
TextView txtCount;
TextView totalCount;
EditText enteramount;
Button btnCount;
Button dmute;
Button dreset;
Button addtotal1;
Button addtotal2;
Button cleartotal;
static int count=0;
static int total=0;
private int x=0;
private int y=0;
private int z=0;
SharedPreferences app_preferences;
MediaPlayer mpButtonClick;
AudioManager audioManager;
public static boolean mutestatus=false;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.durood);

audioManager =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
//SAVE COUNT
app_preferences = this.getSharedPreferences("myPrefscount", MODE_WORLD_READABLE);


count = app_preferences.getInt("count", 0);
total = app_preferences.getInt("total", total++);

txtCount = (TextView)findViewById(R.id.dcount);
txtCount.setText("This app has been started " + count + " times.");

txtCount = (TextView)findViewById(R.id.dcount);
txtCount.setText("This app has been started " + count + " times.");

enteramount = (EditText)findViewById(R.id.enteramount);

totalCount = (TextView)findViewById(R.id.totalCount);
totalCount.setText("This app has been started " + total + " times.");

txtCount = (TextView)findViewById(R.id.dcount);

//Button SOUND AND COUNT
mpButtonClick = MediaPlayer.create(this, R.raw.bubble);

dreset = (Button)findViewById(R.id.dreset);

cleartotal = (Button)findViewById(R.id.cleartotal);

txtCount = (TextView)findViewById(R.id.dcount);
txtCount.setText(String.valueOf(count));

totalCount = (TextView)findViewById(R.id.totalCount);
totalCount.setText(String.valueOf(total));


btnCount = (Button)findViewById(R.id.dclick);

dmute=(Button)findViewById(R.id.dmute);

addtotal1=(Button)findViewById(R.id.addtototal1);
addtotal2=(Button)findViewById(R.id.addtototal2);

btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

count++;
txtCount.setText(String.valueOf(count));
mpButtonClick.start();



}
});

dreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
count = 0;
txtCount.setText("0");
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}
});

cleartotal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
total = 0;
totalCount.setText("0");
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("total", total);
editor.commit();

}
});



dmute.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(!mutestatus){
mutestatus=true;
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);

}
else{
mutestatus=false;
audioManager.setMode(AudioManager.MODE_NORMAL );
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);

}
}});

//add to total 1
addtotal1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

x=Integer.parseInt(txtCount.getText().toString());
y=Integer.parseInt(totalCount.getText().toString());
z=x+y;
totalCount.setText(Integer.toString(z));
count = 0;
txtCount.setText("0");
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}


});

//add to total 2
addtotal2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

x=Integer.parseInt(enteramount.getText().toString());
y=Integer.parseInt(totalCount.getText().toString());
z=x+y;
totalCount.setText(Integer.toString(z));
enteramount.setText("");
}


});
}


@Override
protected void onPause() {
super.onPause();
// save count value here

SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.putInt("total", total);
Log.d("Test", "total: "+total);
editor.commit();

mutestatus=false;
audioManager.setMode(AudioManager.MODE_NORMAL );
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
}

不要使用模式\u WORLD\u READABLE,它不好而且不推荐使用(从最近开始,但无论如何你不应该有任何理由这么做),尝试使用默认的模式\u PRIVATE


除此之外,您的代码应该运行良好。在输入之前验证total的值,并且它是一个整数。您也不需要提交两次编辑器,一次就足够了。

如果您使用的是Eclipse,请使用DDMS透视图下的文件资源管理器在您的计算机上传输包含首选项的文件并检查它。此文件应位于
/data/data/your\u package\u name.your\u application\u name/shared\u prefs


当焦点位于
文件浏览器
选项卡上时,必须使用位于右上角的
从设备中拉出文件。

我终于解决了这个问题。整数“total”被另一个整数“z”填充,请参见上面的完整代码。我把这个也改成了总数,现在它被保存了。感谢大家的帮助。

顺便说一句,您不必提交两次。什么填充了总整数?您应该在onPause中输入log语句,以确保total包含您期望的值<代码>日志d(标签,“总计:+总计)
@twaddington总整数由count int的值和另一个编辑文本字段填充-我有两个“add to total”按钮,当按下它们时,它们会将值添加到总整数中。我尝试添加上面的log语句,但是我得到这个错误“TAG不能由变量解析”。
TAG
只是一个可以在类中定义的字符串常量。您也可以将其替换为“Test”之类的字符串值。是的,当您的设备或仿真器连接到计算机时,打开一个终端,键入
adb logcat
并查找消息。我按照建议进行了更改,但没有起作用,与以前一样。在提交之前,我如何验证“总计”的值?