gpt4 book ai didi

java - SharedPreferences 将不会被保存

转载 作者:行者123 更新时间:2023-12-01 13:50:54 25 4
gpt4 key购买 nike

我正在尝试保存字符串哈哈,但一旦应用程序被销毁并重新启动,我似乎无法使其重新出现,我尝试了多种解决方案,但似乎没有一个对我有用,任何人都可以帮忙?

提前致谢

感谢爱琴海,我找到了解决方案。问题既不是保存也不是获取信息,而是事实是,一旦我检索了保存的信息,我就没有使用它。

代码如下所示:

public class MainActivity extends Activity {

public static final String GEMFFS = "dk.SimonPedersen.GEMFFS";



SharedPreferences ada;
SharedPreferences.Editor editor;
Button btnGem;
ToggleButton tbKon;
EditText etVaegt;
Double sex, vaegt, KT;
String lols, lolzor, KTO;
TextView haha;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ada = getSharedPreferences(GEMFFS, 0);

haha=(TextView)findViewById (R.id.haha);
btnGem = (Button) findViewById (R.id.btnGem);
tbKon = (ToggleButton) findViewById (R.id.tbKon);
etVaegt = (EditText) findViewById (R.id.etVaegt);



tbKon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
sex=0.7d;
} else {
sex=0.6d;
}
Log.d("Konskonstant", "sex" + sex);
}
});



btnGem.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub



lols=etVaegt.getText().toString();
vaegt=Double.parseDouble(lols);
KT = sex*vaegt;
lolzor=KT.toString();
haha.setText("KT " + KT);
}

});

}
protected void onStart(){
super.onStart();
SharedPreferences settings = getApplicationContext().getSharedPreferences(GEMFFS, 0);
String KT = settings.getString("KT", null);
}
protected void onDestroy(){
super.onDestroy();
SharedPreferences settings = getApplicationContext().getSharedPreferences(GEMFFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("KT", lolzor);
editor.commit();
};
}

最佳答案

更改此:

protected void onStart(){
super.onStart();
SharedPreferences settings = getApplicationContext().getSharedPreferences(GEMFFS, 0);
String KT = settings.getString("KT", null);
}

对此:

protected void onStart(){
super.onStart();
SharedPreferences settings = getApplicationContext().getSharedPreferences(GEMFFS, 0);
KTO = settings.getString("KT", null);
KT = Double.valueOf(KTO);

}

还可以在您想要的位置设置此值(到任何 TextView)

注意:您在保存和获取值(value)方面没有任何问题。唯一的问题是当你得到它时你并没有使用它。

关于java - SharedPreferences 将不会被保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19962961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com