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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…