简体   繁体   中英

Shared Preference shows null value when the app rerun after crash

I used sharedPreference to save my object and it works fine but the problem is when the app is crashed it try to run Previous activity in which I call this getQbUser() method shared Preference couldn't find any object any start crashing in loop until I closed the app and rerun my application and it works fine.Shared Preference has the object.

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;

}

//Logs

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)

**Note : The Problem is occur only when the app is crash state and try to run previous activity by itself otherwise there is no issue in my Preference class **

Can you please show your QBUser.class

I think your QBUser class should be like this.

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;
}

You need to add SerialzedName Annonation for converting object to string Your code does not able to convert object to string thats why you getting this error.

For Other people who does'nt know this things please keep it mind.

U can follow this code:

String 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;

}

or your QBUser.class

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

You can use this code.

 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;

}
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM