views:

279

answers:

3

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.

A: 

Just to make sure:

  • In you AndroidManifest you should have the Activity that extends PreferenceActivity with something like this:

    < activity android:name=".activities.Preferences" android:label="@string/app_name"/>

  • The Activity that extends PreferenceActivity 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.

Macarse
I already said I could set and save preferences, and I couldn't have done that without this code in place. So that wasn't it.
Craig
+2  A: 

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-in Preference classes that will store an integer preference, as they mostly store strings.

Your way of accessing the default SharedPreferences (using this) is correct and should not need to be changed.

Here is a sample project showing the use of PreferenceActivity and getting the SharedPreferences back.

CommonsWare
This was the problem. The PreferenceActivity way of creating preferences doesn't let me create an int setting, so that should've been a clue. It's disappointing that I didn't get any kind of meaningful message.I was fooled by the ability to designate a numeric keypad for entry of data in the text field. That had me thinking it was stored as an integer even though it clearly is not.
Craig
A: 

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 ?

fedj
It did crash, and the code presented is where and why it crashed.
Craig