提问人:user8774040 提问时间:10/2/2022 更新时间:10/2/2022 访问量:74
使用我制作的上下文从 sharedPreference 方法获取 Cast 错误
Getting Cast error from a sharedPreference method using context i made
问:
这是我的共享首选项中的代码部分,我从中得到错误:
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")));
}
答: 暂无答案
评论
SharedPreferences
editor.putString(this.context.getString(R.string.Profile_SharedPreferences_Name_Key_Button_A), name);