java将字符串(Cookie)保存到SharedPrefs导致NullPointerException
我用创建了一个类来帮助我处理身份验证(将Cookie保存到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);
}
}
当我调用Authentication.setToken(token)
-方法作为我的响应(RegisterActivity)时,我会得到一个NullPointerException:
java.lang.NullPointerException: Attempt to invoke interface method '安卓.content.SharedPreferences$Editor 安卓.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference
你们谁能帮我解决这个问题吗?提前谢谢
共 (0) 个答案