addPrefencesFromResource 导致 ClassCastException

发布于 2024-10-29 05:12:32 字数 830 浏览 4 评论 0原文

代码

public class Preferences extends PreferenceActivity {
public static final String PREF_AUTO_UPDATE = "PREF_AUTO_UPDATE";
public static final String PREF_MIN_MAG = "PREF_MIN_MAG";
public static final String PREF_UPDATE_FREQ = "PREF_UPDATE_FREQ";

SharedPreferences prefs;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Context context = getApplicationContext();

    addPreferencesFromResource(R.xml.userprefs);


}

每次切换到首选项活动时,我都会收到运行时错误,导致应用程序崩溃。沿着轨迹我得到:

引起:java.lang.ClassCastException:java.lang.Integer

经过一些调试后,我发现注释掉 addPreferencesFromResource 行可以防止应用程序崩溃,但(显然)不会加载任何 UI 。我几乎从教科书中逐行复制这个例子,但我一生都无法弄清楚我错过了什么。

Code:

public class Preferences extends PreferenceActivity {
public static final String PREF_AUTO_UPDATE = "PREF_AUTO_UPDATE";
public static final String PREF_MIN_MAG = "PREF_MIN_MAG";
public static final String PREF_UPDATE_FREQ = "PREF_UPDATE_FREQ";

SharedPreferences prefs;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Context context = getApplicationContext();

    addPreferencesFromResource(R.xml.userprefs);


}

Every time I switch over to the preferences activity, I get a run time error that crashes my app. Down the trace I get:

Caused by: java.lang.ClassCastException: java.lang.Integer

After doing some debugging I found that commenting out the line addPreferencesFromResource will keep the app from crashing, but (obviously) doesn't load any UI. I'm copying this example pretty much line for line out of a text book and can't figure out for the life of me what I am missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-05 05:12:32

我在玩这个东西并击中了类似的东西。结果我正在执行 putInt,但定义首选项的 xml 文件有一个与相关键连接的 TextEditPreference。据我所知,TextEditPreference 坚持通过 putString/getString 与共享首选项文件进行交互(duh),而我在某处执行的 putInt 已将 app-preferences.xml (/data/data/.xml) 中的一个元素卡住了。 ./shared_prefs/..),每次调用 addPreferencesFromResource 时都会终止首选项活动。不确定这是否是您的情况,但我认为一旦您发布用户首选项,人们就可以提供帮助,并注意您是否在代码中自己在 TextEditPreference 的键上使用 putInt 或像我(错误地)所做的那样。

I was playing with this stuff and hit kind of the same thing. Turns out I was doing a putInt but then the xml file defining the preferences had a TextEditPreference hooked up with the relevant key. As far as I can tell, TextEditPreference insists on interacting with the sharedpreferences file via putString/getString (duh), and the putInt I did somewhere in playing around had stuck an element in the app-preferences.xml (/data/data/../shared_prefs/..), which kills the preference activity every time it calls addPreferencesFromResource. Not sure if that's what going on in your case, but I think folks can help once you post your userprefs and also note if you are using putInt on the TextEditPreference's key yourself in code or anything like I was (incorrectly) doing.

人间不值得 2024-11-05 05:12:32

检查您的资源类型。这可能就是问题所在。

Check your resources type. That could be the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文