2013-01-01 23 views
0

我正在创建一个Android应用程序,我有2个静态整数。我想保存这两个int的值,但目前只有1个int被保存。只有1 int使用共享偏好保存

这里是我的拯救整数至今代码:

@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(); 

“计数” 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); 
} 
+1

你不必两次提交btw .. –

+1

什么填充整个int?您应该在onPause中放入一条日志语句,以确保总数包含您期望的值。 'Log.d(TAG,“Total:”+ total);' – twaddington

+0

@twaddington int的总数由count int的值和另一个编辑文本字段填充 - 当按下它们时,我有2'add to total'按钮将该值添加到整个int中。我试着添加上面的日志语句,但是我得到这个错误'TAG无法通过变量解析'。 – Mustafa

回答

0

我终于解决了这个问题。 Int'total'正在被另一个int'z'填充,见上面的完整代码。我改变了这一点,现在它正在被保存。感谢大家的帮助。

1

不要使用MODE_WORLD_READABLE,它没有好和弃用(因为最近,但你不应该”无论如何,没有任何理由这样做),请尝试使用默认的MODE_PRIVATE。

除此之外,你的代码应该工作正常。验证之前合计的值,它是一个整数。你也不需要再犯两次你的编辑器,一次就够了。

+0

我按照建议进行了更改,但它不起作用,与以前一样。如何在提交之前验证“总计”的价值? – Mustafa

0

如果您正在使用Eclipse,请使用DDMS透视图下的文件浏览器将包含您的计算机上的首选项的文件转移并检查它。该文件应位于/data/data/your_package_name.your_application_name/shared_prefs之下。

当焦点位于File Explorer选项卡上时,您必须使用位于右上角的Pull a file from the device