我的代码中存在一些classCastException问题,但我找不到它。我不知道它在哪里。我试图逐行逐行编码,但没有找到。一切似乎都还好。我试图将其与有效的代码进行比较,并且看起来完全一样(值除外)。这是代码:
错误消息的一部分:
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:302)
at com.example.helldumbellv3.Adapters.SharedPreferencesAssistant.getSharedPreferencesInt(SharedPreferencesAssistant.java:79)
at com.example.helldumbellv3.Adapters.RecyclerViewSetter.getObject(RecyclerViewSetter.java:40)
at com.example.helldumbellv3.FitnessActivity.onCreate(MainActivity.java:97)
所以现在代码按照错误消息的顺序>>
MainActivity.java:97:-这在OnCreate方法内部
String recycleViewProgramSelector = RecyclerViewSetter.getObject(1, getApplicationContext());
RecyclerViewSetter.java:40:-这是方法的一部分,因此我将发布整个内容。
public static String getObject(int position, Context context){
/**row n.40 >>*/ int programPositionI = SharedPreferencesAssistant.getSharedPreferencesInt(context,
SharedPreferencesAssistant.SharedPreferencesKeys.RecyclerSTposition, 999);
int programPositionII = SharedPreferencesAssistant.getSharedPreferencesInt(context,
SharedPreferencesAssistant.SharedPreferencesKeys.RecyclerNDposition, 999);
int programPositionIII = SharedPreferencesAssistant.getSharedPreferencesInt(context,
SharedPreferencesAssistant.SharedPreferencesKeys.RecyclerRDposition, 999);
if (position == programPositionI) {
return SharedPreferencesAssistant.getSharedPreferencesString(context,
SharedPreferencesAssistant.SharedPreferencesKeys.RecyclerViewST, "nothing");
}
else if (position == programPositionII) {
return SharedPreferencesAssistant.getSharedPreferencesString(context,
SharedPreferencesAssistant.SharedPreferencesKeys.RecyclerViewND, "nothing");
}
else if (position == programPositionIII) {
return SharedPreferencesAssistant.getSharedPreferencesString(context,
SharedPreferencesAssistant.SharedPreferencesKeys.RecyclerViewRD, "nothing");
}
return "Error - you have to set position to number 1,2 or 3";
}
SharedPrefferencesAssistant.java:79:-这也是方法的一部分,因此这里是整个方法
public static int getSharedPreferencesInt(Context context, String key, int _default){
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(context);
/**row n.79 >>*/ return preferences.getInt(key, _default);
}
这些是上面代码中使用的键:
public static class SharedPreferencesKeys{
public static final String RecyclerViewST = "1";
public static final String RecyclerSTposition = "non";
public static final String RecyclerViewND = "2";
public static final String RecyclerNDposition = "non";
public static final String RecyclerViewRD = "3";
public static final String RecyclerRDposition = "non";
public static final String activatedProgram= "0";
}
我认为问题出在RecyclerViewSetter.java中,因为在创建此方法之前,我创建的最后一个类和SharedPreferencesAssistant类运行良好。
看到这个。似乎已在类似情况下得到解决。
sharedPref.getInt: java.lang.String cannot be cast to java.lang.Integer
尝试使用:String.valueOf(integerValue)