我试图在共享首选项中保存一个整数值,并在重新启动应用程序时再次加载它.
我在主要活动中创建了一个共享首选项,并尝试在菜单项单击的另一个Menu Listener类中保存整数值. Menu侦听器类位于不同的包中,Menu Listener类扩展了Main Activity.
但是当我尝试这样做时,我得到一个Null指针异常.
我也经历了这个link!但它似乎没有帮助我.
这是我的代码:
主要活动:
protected SharedPreferences mPrefs;protected SharedPreferences.Editor mEditor;protected Context main_activityclass_context; //EDITpublic static int count=0;@OverrIDeprotected voID onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPrefs = this.getSharedPreferences("theme", 0); mEditor = mPrefs.edit(); }
菜单监听器类:
case mainActivity.MENU_ITEM_theme_DARK: { mainActivity.count=1; mPrefs =main_activityclass_context.getSharedPreferences("theme", 0); //EDIT mEditor = mPrefs.edit(); mEditor.putInt("theme_count", mainActivity.count).commit(); break; }
我在菜单监听器的行上得到一个Null指针异常:
mPrefs = getSharedPreferences("theme", 0); mEditor = mPrefs.edit();
我的Logcat:
08-03 10:54:03.650: E/AndroIDRuntime(31258): FATAL EXCEPTION: main 08-03 10:54:03.650: E/AndroIDRuntime(31258): java.lang.NullPointerException 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.content.Contextwrapper.getSharedPreferences(Contextwrapper.java:153) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at de.exb.ptpt.tablet.menu.tabletMenuListener.onMenuItemClick(tabletMenuListener.java:215) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at com.androID.internal.vIEw.menu.MenuItemImpl.invoke(MenuItemImpl.java:144) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at com.androID.internal.vIEw.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at com.androID.internal.vIEw.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.Widget.AdapterVIEw.performItemClick(AdapterVIEw.java:292) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.Widget.AbsListVIEw.performItemClick(AbsListVIEw.java:1058) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.Widget.AbsListVIEw$PerformClick.run(AbsListVIEw.java:2514) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.Widget.AbsListVIEw.run(AbsListVIEw.java:3168) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.os.Handler.handleCallback(Handler.java:605) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.os.Handler.dispatchMessage(Handler.java:92) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.os.Looper.loop(Looper.java:137) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at androID.app.ActivityThread.main(ActivityThread.java:4424) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at java.lang.reflect.Method.invokeNative(Native Method) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at java.lang.reflect.Method.invoke(Method.java:511) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551) 08-03 10:54:03.650: E/AndroIDRuntime(31258): at dalvik.system.NativeStart.main(Native Method)
谁能帮帮我吗?
谢谢.
解决方法:
其中一个好方法是,创建一个帮助类来声明,保存和检索首选项.您可以参考以下代码示例:
public class PreferencesHelper {private SharedPreferences sharedPreferences;private Editor editor;public PreferencesHelper(Context context) {this.sharedPreferences = getPreferences(MODE_PRIVATE); this.editor = sharedPreferences.edit(); }public String GetPreferences(String key) { return sharedPreferences.getString(key, "");}public voID SavePreferences(String key, String value) {editor.putString(key, value); editor.commit(); }}
在活动的onCreate事件中,您可以创建PreferencesHelper的对象并获取所有已保存的首选项,如:
Prefs = new PreferencesHelper(getApplicationContext()); Prefs.GetPreferences("YOURARGUMENT");
并且您可以保存以下偏好:
Prefs.SavePreferences(key, value);
希望这会帮助你.
总结以上是内存溢出为你收集整理的Android Null Pointer共享首选项的例外全部内容,希望文章能够帮你解决Android Null Pointer共享首选项的例外所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)