I have a app with 2 tabbed activities, and I’m trying to get SharedPreferences on the second tab by using this code: SharedPreferences prefs = this.getSharedPreferences("com.some.app", Context.MODE_PRIVATE);
But it makes the app force close, I use the same code on the first activity and it gets the SharedPreferences just fine! Any ideas what causing this?
Logcat:
06-19 17:10:37.690: W/dalvikvm(6589): threadid=1: thread exiting with uncaught exception (group=0x40a031f8)
06-19 17:10:37.706: E/AndroidRuntime(6589): FATAL EXCEPTION: main
06-19 17:10:37.706: E/AndroidRuntime(6589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.something.some/com.something.somehow}: java.lang.NullPointerException
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.widget.TabHost.setCurrentTab(TabHost.java:346)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.view.View.performClick(View.java:3511)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.view.View$PerformClick.run(View.java:14105)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.os.Handler.handleCallback(Handler.java:605)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.os.Looper.loop(Looper.java:137)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-19 17:10:37.706: E/AndroidRuntime(6589): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 17:10:37.706: E/AndroidRuntime(6589): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 17:10:37.706: E/AndroidRuntime(6589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
06-19 17:10:37.706: E/AndroidRuntime(6589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
06-19 17:10:37.706: E/AndroidRuntime(6589): at dalvik.system.NativeStart.main(Native Method)
06-19 17:10:37.706: E/AndroidRuntime(6589): Caused by: java.lang.NullPointerException
06-19 17:10:37.706: E/AndroidRuntime(6589): at com.someone.someapp.Statify.onCreate(Statify.java:59)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.app.Activity.performCreate(Activity.java:4465)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-19 17:10:37.706: E/AndroidRuntime(6589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-19 17:10:37.706: E/AndroidRuntime(6589): ... 18 more
Tab Host houses seperate activites(if im not mistaken) so when you call “this” it is returning the context of the tab activity, why it null i am not sure(i am assuming this is null because idk what is on line 59).
My recommendation would be to use one shared preferences instance from your main activity instead of initiating a shared preference variable for each sub activity(Even though they would point to the same prefs)
Or better yet use a ViewPager instead of the outdated Tab Host.
If you want to stick with what you are doing try this.getBaseContext().getSharedPreferences()
More contextual information would allow for a better answer as your smippit is correct.