Save String (Cookie) to SharedPrefs caused NullPointerException

柔情痞子 提交于 2019-12-13 23:09:36

问题


I´ve made a class with helps me to handle the Authentication (save Cookie to SharedPrefs).

public class Authentication extends Application {

    String PREFS_NAME = "UserData";
    String DEFAULT = "";

    Context context;
    public static SharedPreferences sharedPreferences;
    public static SharedPreferences.Editor editor;
    public static String token;

    public Authentication(Activity context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        token = sharedPreferences.getString("Cookie", DEFAULT);
    }

    //speichert Token in den Shared Preferences
    public static void setToken(String token) {
        Log.d("Cookie", token);
        editor.putString("Cookie", token);
        }
}

When I call the Authentication.setToken(token)-method my response (RegisterActivity) I´ll get a NullPointerException:

java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference

Can someone of you help me to solve this prob? Thanks in advance


回答1:


you are not register your application in manifiest that. or first createonject of Authentication with your code first register it in manifiest

change your Authentication with

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class Authentication extends Application {
    String PREFS_NAME = "UserData";
    String DEFAULT = "";
    Context context;
    public static SharedPreferences sharedPreferences;
    public static SharedPreferences.Editor editor;
    public static String token;

    public Authentication() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context  = this;
        sharedPreferences = getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        token = sharedPreferences.getString("Cookie", DEFAULT);
    }

    // speichert Token in den Shared Preferences
    public static void setToken(String token) {
        Log.d("Cookie", token);
        if(editor==null){
            throw new NullPointerException("Register your application "+Authentication.class+" in AndroidManifiest.xml");
        }
        editor.putString("Cookie", token);
    }

}

AndroidManifiest.xml

<application
        android:name="com.android.Authentication"
        android:icon="@mipmap/ic_launcher_home"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.blue"
         >
.
.
.
 <activity.../>
<service.../>

  </application>


来源:https://stackoverflow.com/questions/37924119/save-string-cookie-to-sharedprefs-caused-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!