当应用程序在崩溃后重新运行时,共享首选项显示空值

时间:2017-01-18 06:27:19

标签: java android sharedpreferences

我使用sharedPreference来保存我的对象并且它工作正常但问题是当应用程序崩溃时它尝试运行以前的活动我称之为getQbUser()方法共享首选项无法找到任何对象在循环中崩溃,直到我关闭应用程序并重新运行我的应用程序,它工作正常.Shared Preference有对象。

public void saveQbuser(QBUser user){
    SharedPreferences.Editor prefsEditor = mPref.edit();
    Gson gson = new Gson();
    String json = gson.toJson(user);
    prefsEditor.putString("qbuser", json);
    prefsEditor.commit();
}

public QBUser getQbUser(){
    Gson gson = new Gson();
    String json = mPref.getString("qbuser","");
    QBUser obj = gson.fromJson(json, QBUser.class);
    return obj;

}

//日志

Caused by java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
    com.attribes.push2beat.Utils.DevicePreferences.getQbUser (DevicePreferences.java:86)
    com.attribes.push2beat.mainnavigation.SelectActivity.createChatService (SelectActivity.java:38)
    com.attribes.push2beat.mainnavigation.SelectActivity.onCreate (SelectActivity.java:27)
    android.app.Activity.performCreate (Activity.java:6251)
    android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1107)
    android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2369)
    android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2476)
    android.app.ActivityThread.-wrap11 (ActivityThread.java)
    android.app.ActivityThread$H.handleMessage (ActivityThread.java:1344)
    android.os.Handler.dispatchMessage (Handler.java:102)
    android.os.Looper.loop (Looper.java:148)
    android.app.ActivityThread.main (ActivityThread.java:5417)
    java.lang.reflect.Method.invoke (Method.java)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
    com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)

**注意:问题仅在应用程序处于崩溃状态时发生并尝试自行运行上一个活动,否则我的Preference类中没有问题**

3 个答案:

答案 0 :(得分:0)

你能否展示你的QBUser.class

我认为你的QBUser类应该是这样的。

public class QBUser {

@SerializedName("name")
String name;

@SerializedName("address")
String address;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

您需要添加SerialzedName Annonation以将对象转换为字符串 您的代码无法将对象转换为字符串,这就是您收到此错误的原因。

对于那些不了解这些事情的人,请记住。

答案 1 :(得分:0)

你可以遵循以下代码:

字符串PREFS_NAME =“QBUser”;

 public void saveQbuser(Context context,QBUser user) {
    Editor editor = context.getSharedPreferences(PREFS_NAME,
            Context.MODE_PRIVATE).edit();
    Gson gson = new Gson();
        String json = gson.toJson(user);
        prefsEditor.putString("qbuser", json);
        prefsEditor.commit();

}
public QBUser getQbUser()
{

 SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        Gson gson = new Gson();
String json = preferences.getString("qbuser","");
QBUser obj = gson.fromJson(json, QBUser.class);
return obj;

}

或您的QBUser.class

public class QBUser
   {
   // do here code setter or getter method of jsonString or objects.
      }

答案 2 :(得分:0)

您可以使用此代码。

 class PrefsHelper{


    private SharedPreference mPref;
    private Editor edit;
    private static final PREFS_NAME = "myprefs";


    public PrefsHelper(Context context){
     mPref = context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE);
    edit = mPref.edit();
    }

     public void saveQbuser(QBUser user)
        {

            Gson gson = new Gson();
            String json = gson.toJson(user);
            edit.putString("qbuser", json);
            edit.commit();

    }


public QBUser getQbUser()
{
    Gson gson = new Gson();
    String json = mPref.getString("qbuser","");
    QBUser obj = gson.fromJson(json, QBUser.class);
    return obj;

}
    }