public void onClick(View v) {
switch(v.getId()){
case R.id.save2:
s = text.getText().toString();
number = Float.parseFloat(num.getText().toString());
SharedPreferences.Editor editor = shared.edit();
editor.putString("sharedtext",s);
editor.putFloat("sharednum",number);
editor.commit();
Toast.makeText(this,"save2",Toast.LENGTH_SHORT).show();
break;
case R.id.load2:
String returner = shared.getString("sharedtext","Returner fail");
float returnnum = shared.getFloat("sharednum","number fail");
//there is a red mark here^^^^^^^^^(getfloat)
text.setText(returner);
//num.set.......how will
Toast.makeText(this,"load2",Toast.LENGTH_SHORT).show();
break;
case R.id.page2:
intent= new Intent(this,SharedPreferencesActivity.class);
startActivity(intent);
Toast.makeText(this,"page2",Toast.LENGTH_SHORT).show();
break;
}
//这里有一个红色标记^^^^^^^^^(getfloat) - 让我切换到getInt和getFloat的错误
// num.set .......我将如何设置数值?
感谢
答案 0 :(得分:1)
当您尝试从首选项获取值时,还需要设置默认值,如下所示:
float returnnum = shared.getFloat("sharednum",0.0);
答案 1 :(得分:0)
float returnnum = shared.getFloat("sharednum","number fail");
将“数字失败”更改为多个类型float
。此参数表示默认值,因此您不能使用字符串。
答案 2 :(得分:0)
试试这个
float returnnum = shared.getFloat("sharednum",0f);