使用我制作的上下文从 sharedPreference 方法获取 Cast 错误

Getting Cast error from a sharedPreference method using context i made

提问人:user8774040 提问时间:10/2/2022 更新时间:10/2/2022 访问量:74

问:

这是我的共享首选项中的代码部分,我从中得到错误:

package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferenceHelper {

    private final SharedPreferences sharedPreferences;
    private final Context context;

    public SharedPreferenceHelper(Context context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences(this.context.getString(R.string.Profile_SharedPreferences), Context.MODE_PRIVATE);
    }

    public String getName(String key) {
        switch (key) {
            case "A":
                return this.sharedPreferences.getString((this.context.getString(R.string.Profile_SharedPreferences_Name_Key_Button_A)), "");
            case "B":
                return this.sharedPreferences.getString((this.context.getString(R.string.Profile_SharedPreferences_Name_Key_Button_B)), "");
            case "C":
                return this.sharedPreferences.getString((this.context.getString(R.string.Profile_SharedPreferences_Name_Key_Button_C)), "");
            default:
                return "Error";
        }

    }

我尝试调试,但我不明白为什么这会导致异常错误,我希望在以下行: 调试以查找是否检测到密钥? 这与我在 resources 中的密钥名称匹配: resources

this.sharedPreferences.getString((this.context.getString(R.string.Profile_SharedPreferences_Name_Key_Button_A)), "");

返回一个 String 值,当我进入调试模式时,我看到以下内容:

2022-10-01 12:24:26.731  9722-9722  AndroidRuntime          com.example.myapplication            E  FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 9722
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:302)
    at com.example.myapplication.SharedPreferenceHelper.getName(SharedPreferenceHelper.java:19)
    at com.example.myapplication.MainActivity.onStart(MainActivity.java:40)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
    at android.app.Activity.performStart(Activity.java:8024)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

这也是我从 Main 调用方法的地方:

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferenceHelper sharedPreferenceHelper = new SharedPreferenceHelper(getApplicationContext());
        eventA_Button.setText(String.valueOf(sharedPreferenceHelper.getName("A")));
}
java android-studio 投射 sharedpreferences

评论

1赞 CommonsWare 10/2/2022
显然,在过去某个时候,您使用该键保存了一个整数。清除应用的数据,或卸载/重新安装应用,看看是否能解决您的问题。SharedPreferences
0赞 user8774040 10/2/2022
返回mainActivity页面后仍然崩溃,并出现相同的错误。
0赞 user8774040 10/2/2022
这就是我在代码中设置名称的方式editor.putString(this.context.getString(R.string.Profile_SharedPreferences_Name_Key_Button_A), name);

答: 暂无答案