【问题标题】:Android Crash after using SharedPreferences使用 SharedPreferences 后的 Android 崩溃
【发布时间】:2013-05-04 01:42:47
【问题描述】:

我正在开发一个 Android 应用程序,为了让我保持登录功能,我正在使用 SharedPreferences 应用程序开始崩溃。

这是我为首选项添加的行;

SharedPreferences pref = getApplicationContext().getSharedPreferences("RS_Remember" , getApplicationContext().MODE_PRIVATE);

我很确定这条线会导致错误,因为如果我删除它就可以正常工作。

我收到的错误消息说:

java.lang.Runtime:无法创建应用程序 com.x.x.x:java.lang.NullPointerException。

知道我应该做什么吗?

编辑

public Activity activity;
    SharedPreferences pref;
    private RSCurrentUserManager()
    {
        super();

            pref = this.activity.getSharedPreferences("RallySpark_Remember",0);


    }


+05-09 18:02:56.221: E/AndroidRuntime(31760): FATAL EXCEPTION: main
+05-09 18:02:56.221: E/AndroidRuntime(31760):
 java.lang.RuntimeException: Unable to create application
 com.x.x.general.RSApplication:
 java.lang.NullPointerException
+05-09 18:02:56.221:
 E/AndroidRuntime(31760):   at
 com.x.x.general.RSCurrentUserManager.<init>(RSCurrentUserManager.java:93)
+05-09 18:02:56.221: E/AndroidRuntime(31760):   at com.x.x.general.RSCurrentUserManager.getInstance(RSCurrentUserManager.java:84)

我没有在任何其他方面使用共享首选项来查找问题,我删除了除其定义之外的所有内容

【问题讨论】:

  • 尝试用你的Activity context替换getApplicationCOntext()
  • getApplicationContext() 是否返回 null ?
  • 不要使用getApplicationContext(),而是使用上下文变量,你需要从你的活动中传递,context.getSharedPreferences()
  • 知道我应该做什么吗? 是的。您应该阅读崩溃时在日志中创建的堆栈跟踪。此跟踪将指出可能的故障点。如果之后您仍然需要帮助,请发布跟踪和相关代码。
  • 我已经尝试过使用 Activit 上下文,但没有帮助。为了了解 getApplicationContext() 是否为空,我将代码替换为 if(getApplicationContext() != null) { pref = getApplicationContext().getSharedPreferences("RS_Remember" , getApplicationContext().MODE_PRIVATE); } 它仍然无法正常工作

标签: android nullpointerexception sharedpreferences


【解决方案1】:

如果您在 Activity 方法中,那么您甚至不需要使用 Context。如果您不在内部类或侦听器中或不在内部类或侦听器中,则复制

SharedPreferences pref = getApplicationContext().getSharedPreferences("RS_Remember" , getApplicationContext().MODE_PRIVATE);

SharedPreferences pref = ActivityName.this.getSharedPreferences("RS_Remember" , getApplicationContext().MODE_PRIVATE);

大多数时候你会想要使用Activity context 而不是Application context。您可以在 SO 上找到许多关于这两者的良好信息的帖子。我很确定这是您的问题,但如果不是,请发布完整的 logcat 并显示此行在您的代码中的位置。

This SO answer对两者都有很好的解释

【讨论】:

  • 就像你告诉我的我不在活动课上,这是我在Activity activity; SharedPreferences pref = activity.getSharedPreferences("RS_Remember" , activity.getApplicationContext().MODE_PRIVATE); 之前尝试过的,它给了我相同的结果。我尝试了你的代码,但它没有工作仍然同样的错误
  • this 传递给您尝试使用它的类并将其分配给Context 变量,然后改用该变量。如果这不起作用,请“发布完整的 logcat 并显示你在哪里调用它”
  • 该类的第 93 行是什么?此外,您似乎没有将任何内容分配给 activity
【解决方案2】:

您可以使用 SharedPreferences 即

SharedPreferences preference=null; 


@Override
protected void onCreate(Bundle arg0) {
preference= this.getSharedPreferences("RS_Remember", MODE_PRIVATE);
if(rememberMe.isChecked()){

                SharedPreferences.Editor editor = preference.edit();
                editor.putString("UserName", name.getText().toString());
                editor.putString("Password", password.getText().toString());


                // Commit the edits!
                editor.commit();


        }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2016-08-20
    • 2016-04-26
    • 2014-03-09
    相关资源
    最近更新 更多