gpt4 book ai didi

java - 共享首选项 edittext settext 错误

转载 作者:行者123 更新时间:2023-12-02 03:55:16 25 4
gpt4 key购买 nike

Java代码

private void sharedpreferenceAl() {

SharedPreferences sp = getSharedPreferences("geri", Context.MODE_PRIVATE);
git.setText(String.valueOf(sp.getInt("salak", 1)));

}



private void sharedpreferencekaydet() {
SharedPreferences sp = getSharedPreferences("geri", Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sp.edit();
prefEditor.putString("salak", git.getText().toString());
prefEditor.commit();
Log.i("tago", "aaaaaaaaaaa");
gitbak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

sharedpreferenceAl();

}

});

日志猫

致命异常:主要

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:239)
at kporejct_replics.sunnetler.MainActivity.sharedpreferenceAl(MainActivity.java:123)
at kporejct_replics.sunnetler.MainActivity.access$100(MainActivity.java:23)
at kporejct_replics.sunnetler.MainActivity$3.onClick(MainActivity.java:102)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最佳答案

更改下面的行 -

git.setText(String.valueOf(sp.getInt("salak", 1)));

到-

git.setText(sp.getString("salak", "1"));

关于java - 共享首选项 edittext settext 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35546040/

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