Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我在这里得到一个ClassCastException?_Java_Android_Preferences_Classcastexception_Preferenceactivity - Fatal编程技术网

Java 为什么我在这里得到一个ClassCastException?

Java 为什么我在这里得到一个ClassCastException?,java,android,preferences,classcastexception,preferenceactivity,Java,Android,Preferences,Classcastexception,Preferenceactivity,我正在创建一个android应用程序,并尝试使用PreferenceActivity 我得到java.lang.ClassCastException:java.lang.String错误,当它到达这一行时return PreferenceManager.getDefaultSharedReferences(context).getInt(USER\u TERM,USER\u TERM\u DEF) 我想这可能是因为我没有正确地将它从EditText框中的字符串值转换为我需要的int,但我不知道如

我正在创建一个android应用程序,并尝试使用PreferenceActivity

我得到java.lang.ClassCastException:java.lang.String错误,当它到达这一行时
return PreferenceManager.getDefaultSharedReferences(context).getInt(USER\u TERM,USER\u TERM\u DEF)

我想这可能是因为我没有正确地将它从EditText框中的字符串值转换为我需要的int,但我不知道如何解决这个问题,或者这就是原因?我很困惑

以下是我的首选活动课程:

public class UserPrefs extends PreferenceActivity {
//option names and default vals
private static final String USER_TERM = "term_length";
private static final String USER_YEAR = "year_length";
private static final int USER_TERM_DEF = 12;
private static final int USER_YEAR_DEF = 34;

@Override 
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences); 
}

public static int getTermLength(Context context){
    try{
    return PreferenceManager.getDefaultSharedPreferences(context).getInt(USER_TERM, USER_TERM_DEF);
    }catch (ClassCastException e){Log.v("getTermLength", "error::: " + e); }
    //return 1;//temporary, needed to catch the error and couldn't without adding a return outside the block.//
}
public static int getYearLength(Context context){
    return PreferenceManager.getDefaultSharedPreferences(context).getInt(USER_YEAR, USER_YEAR_DEF);
}
下面是我试图在另一个类中使用首选项的代码:

public float alterForFrequency(Context keypadContext, float enteredAmount, String spinnerPosition){

    int termLength = UserPrefs.getTermLength(keypadContext);
    int yearLength = UserPrefs.getYearLength(keypadContext);
}

完整的android logcat,我上传到这里:

您有一个首选项,它存储为
字符串,您试图将其作为
int
读入,因此出现了
ClassCastException

如果您愿意将首选项存储为字符串,则只需使用
getString
并将该值传递给
Integer.parseInt()
即可获得
int


否则,您需要确保将该值作为int写入首选项。

您可以发布完整的stacktrace吗?
首选项管理器.getDefaultSharedReferences(上下文)
的返回时间是多少?
getInt()
方法的签名是什么?@Hunter2,该死。对不起,我是编程新手,不好意思,我不能回答这些问题:/。但是如果有帮助的话,我可以发布androids logcat(我有)的内容,如果您愿意告诉我如何在eclipse中获取stacktrace,我也会这样做。无论如何谢谢。我肯定@Hunter2是指
ClassCastException
的堆栈跟踪。您可以删除try/catch,然后发布日志。无论如何,它是
getInt(String,int)
,所以这应该是正确的。