Cannot access settings created by PreferenceActivity

I have a minimal program that does a little more than allows me to set two settings (int and String) using Android PreferenceActivity. Thus, I have an xml file that defines my settings and activity that extends PreferenceActivity. In my main activity, there is an options menu that launches my preferences activity. This all works great. I can set my settings and the values ​​are saved during and after the execution of my program.

Now in my main activity I want to get these settings. It 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", "");

The problem is that if I break the call getInt()and step over, my stack looks like this and the application will crash if I continue:

Topic [<3> main] (paused)
ActivityThread.performLaunchActivity (ActivityThread $ ActivityRecord, Intent): 2494
ActivityThread.handleLaunchActivity (ActivityThread $ ActivityRecord, Intent): 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 (line []): 4363 Method.invokeNative (Object, Object [], Class, Class [], Class, int, boolean): not available [native method] Method.invoke (Object, Object ...): 521
ZygoteInit $ MethodAndArgsCaller.run () string: 860 ZygoteInit.main (String []) string: 618 NativeStart.main (String []) string: not available [native method]

I tried options for the parameter passed to getDefaultSharedPreferences (), including:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

... and maybe some others in different contexts (i.e. when accessing the preferences from my main activity compared to another activity compared to some function in the class that is not activity. The result is always the same.

I read a dozen Q&A messages on the Internet from others with this problem who solved it by writing code identical to mine, so I do not expect that there will be a solution, but if anyone has any ideas, let me know.

+3
3

. Thread [<3> main] (Suspended) - Eclipse. , , Android " ", LogCat ( DDMS) .

, intSetting, , . Preference, , .

SharedPreferences ( this) .

, PreferenceActivity SharedPreferences .

+7

, :

  • AndroidManifest Activity, PreferenceActivity :

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

  • Activity, PreferenceActivity, :

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

R.layout.prefs - XML-.

, xml, , ppl .

0

This will not work, even if your preferences were not saved, I think that you missed some code, and from this point there was a failure. Do you have other topics?

0
source

Source: https://habr.com/ru/post/1759969/


All Articles