无法访问 PreferenceActivity 创建的首选项
我有一个最小的程序,它的作用只不过是让我使用 Android 的 PreferenceActivity
设置两个首选项(一个 int 和一个 String)。因此,我有一个定义我的首选项的 xml 文件,以及一个扩展 PreferenceActivity
的活动。我的主要活动有一个选项菜单,可启动我的偏好活动。所有这些都非常有效。我可以设置我的首选项,并且在程序执行期间和之后保留这些值。
现在,在我的主要活动中,我想检索这些首选项。应该很容易吧?以下是我见过的每个示例的代码:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int intSetting = prefs.getInt("intSetting", 0);
String strSetting = prefs.getString("strSetting", "");
问题是,如果我在 getInt()
调用处中断并单步执行,我的堆栈将如下所示,如果我继续,应用程序将崩溃:
线程 [<3>主](暂停)
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, 意向)线路:2494
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, 意向线:2512
ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord,意图) 线路:119
ActivityThread$H.handleMessage(消息) 线路:1863
ActivityThread$H(处理程序).dispatchMessage(消息) 行:99 Looper.loop() 行:123
ActivityThread.main(String[]) 行: 第4363章 对象[]、类、类[]、类、int、 布尔)行:不可用 [native 方法] Method.invoke(对象, 对象...)行:521
ZygoteInit$MethodAndArgsCaller.run() 行:860 ZygoteInit.main(字符串[]) 行:618 NativeStart.main(字符串[]) 行:不可用[本机方法]
我已经尝试过传递给 getDefaultSharedPreferences() 的参数的变体,包括:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
...以及可能在不同上下文中的其他一些(即,当从我的主要活动与另一个活动与某些活动访问首选项时) 结果总是相同的。
我在网上阅读了许多其他人通过编写与我相同的代码解决该问题的问答帖子, 不指望有解决方案,但如果有人有任何想法,请告诉我。
I have a minimal program that does little more than let me set two preferences (an int and a String) using Android's PreferenceActivity
. So I have an xml file that defines my preferences, and an activity that extends PreferenceActivity
. My main activity has an options menu that launches my preference activity. All of that works great. I can set my preferences and the values are retained during and after my program executes.
Now, in my main activity I want to retrieve those preferences. Should be easy, right? Here's the code from every sample I've ever seen:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int intSetting = prefs.getInt("intSetting", 0);
String strSetting = prefs.getString("strSetting", "");
Problem is, if I break at the getInt()
call and step over, my stack looks like this and the app will crash if I continue:
Thread [<3> main] (Suspended)
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord,
Intent) line: 2494
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord,
Intent) line: 2512
ActivityThread.access$2200(ActivityThread,
ActivityThread$ActivityRecord, Intent)
line: 119
ActivityThread$H.handleMessage(Message)
line: 1863
ActivityThread$H(Handler).dispatchMessage(Message)
line: 99 Looper.loop() line: 123
ActivityThread.main(String[]) line:
4363 Method.invokeNative(Object,
Object[], Class, Class[], Class, int,
boolean) line: not available [native
method] Method.invoke(Object,
Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run()
line: 860 ZygoteInit.main(String[])
line: 618 NativeStart.main(String[])
line: not available [native method]
I've tried variations on the parameter passed to getDefaultSharedPreferences(), including:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
...and probably some others in different context (i.e. when accessing preferences from my main activity vs. another activity vs. some function in a class that isn't an activity. The result is always the same.
I've read a dozen Q&A posts on the Web from others with this problem who have solved it by writing code identical to mine, so I don't expect there's a solution but if anyone has any ideas, let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不显示堆栈跟踪。
线程[<3> main] (Suspended)
是 Eclipse 的无用输出。您需要检查真实的堆栈跟踪,您应该通过允许 Android 继续“强制关闭”对话框来获取该堆栈跟踪,然后查看 LogCat(在您的 DDMS 视角中)以获取堆栈跟踪。我的猜测是,您有一个名为
intSetting
的首选项,但它不是整数,但这只是一个猜测。我即兴想到任何内置的 Preference 类将存储整数首选项,因为它们主要存储字符串。您访问默认
SharedPreferences
的方式(使用this
)是正确的,不需要更改。这是一个示例项目,展示了
的使用PreferenceActivity
并获取SharedPreferences
。You do not show the stack trace. The
Thread [<3> main] (Suspended)
is useless output from Eclipse. You need to examine the real stack trace, which you should get by allowing Android to continue to the "Force Close" dialog, then looking at LogCat (in your DDMS perspective) for the stack trace.My guess is that you have a preference named
intSetting
but it is not an integer, but that is just a guess. Off the cuff, I cannot think of any of the built-inPreference
classes that will store an integer preference, as they mostly store strings.Your way of accessing the default
SharedPreferences
(usingthis
) is correct and should not need to be changed.Here is a sample project showing the use of
PreferenceActivity
and getting theSharedPreferences
back.只是为了确保:
在您的
AndroidManifest
中,您应该具有扩展PreferenceActivity
的Activity
,如下所示:<活动 android:name=".activities.Preferences"
android:label="@string/app_name"/>
扩展
PreferenceActivity
的Activity
应具有以下代码:@覆盖
protected void onCreate(Bundle savingInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.prefs);
}
R.layout.prefs
是您的首选项的 xml。如果您已完成这两件事,请使用您正在使用的 xml 编辑您的问题,以便 ppl 可以重现您的问题。
Just to make sure:
In you
AndroidManifest
you should have theActivity
that extendsPreferenceActivity
with something like this:< activity android:name=".activities.Preferences"
android:label="@string/app_name"/>
The
Activity
that extendsPreferenceActivity
should have the following code:@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.prefs);
}
where
R.layout.prefs
is your preferences' xml.If you have done both things, please edit your question with the xml you are using so ppl can reproduce your issue.
即使您的首选项未保存,它也不会崩溃,我认为您错过了一些代码,并且崩溃不是来自这一点。你还有其他线程吗?
It wouldn't crash, even if your preference was not saved, I think you missed some code and the crash does not come from this point. Do you have some other threads ?