-1

クラスを作成して、認証(CookieをSharedPrefsに保存)を処理するのに役立ちました。SharedPrefsに文字列(Cookie)を保存すると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); 
     } 
} 

私はAuthentication.setToken(token)が私の応答を呼び出すと方法 - (RegisterActivity) - 私は、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

あなたの誰かがこのPROBを解決するために私を助けることができますか?事前に感謝します

+1

は、メソッドからstaticキーワードを削除します'setToken'と試してください –

+0

どこにあなたは 'String token'を取得していますか? –

+0

私は 'setToken()'を呼び出す前に 'Authentication()'を呼び出さずにいると思います... 'editor'は初期化されていません – Opiatefuchs

答えて

2

あなたはそれを明示的に登録していません。

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であなたのコード それを最初に登録しmanifiestで

変更あなたの認証認証の または最初createonjectは

<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 ...この方法ではNullPointerExceptionはスローされませんが、私のトークンはSharedPrefs ...任意の提案に保存されませんか? – Andy

+0

ああ...心配しないで!あなたは 'editor.apply()'を呼ぶのを忘れてしまった!今それは完璧に動作します、ありがとう:) – Andy

関連する問題