我正在尝试保存 String lolzor,但是一旦应用程序被销毁并重新启动,我似乎无法让它重新出现,我尝试了多种解决方案,但它们似乎都不适合我,有人可以帮忙吗?
提前致谢
感谢爱琴海,我找到了解决方案。问题既不是保存也不是获取信息,而是一旦我检索到保存的信息,我就没有使用它。
代码如下所示:
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();
};
}