-1

Ich habe eine Klasse erstellt, die mir bei der Authentifizierung hilft (Cookie in SharedPrefs speichern).Zeichenfolge (Cookie) in SharedPrefs speichern Ursache NullPointerException

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

als ich den Anruf Authentication.setToken(token) -Methode meine Antwort (RegisterActivity) Ich werde eine Nullpointer erhalten:

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

Kann jemand von euch mir helfen, dieses prob zu lösen? Vielen Dank im Voraus

+1

Schlüsselwort static von Verfahren entfernen 'setToken' und versuchen –

+0

wo sind bekommst du 'String token' von? –

+0

Ich denke, Sie rufen 'setToken()' ohne Aufruf von 'Authentication()' vor ... so wird Ihr 'Editor' nicht initialisiert – Opiatefuchs

Antwort

2

Sie registrieren Ihre Anwendung nicht in Manifiest das. oder erste createonject von Authentication mit Ihrem Code zuerst manifiest

Ihre Authentication mit

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

Änderung

in registrieren
<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> 
+0

ok ... auf diese Weise wird keine NullPointerException ausgelöst, aber mein Token wird nicht in den SharedPrefs gespeichert ... irgendeinen Vorschlag? – Andy

+0

ah ... Alles klar! Sie haben vergessen, 'editor.apply()' aufzurufen! Jetzt funktioniert es perfekt, danke :) – Andy