Skip to content Skip to sidebar Skip to footer

Save String (cookie) To Sharedprefs Caused Nullpointerexception

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';

Solution 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;

publicclassAuthenticationextendsApplication {
    StringPREFS_NAME = "UserData";
    StringDEFAULT = "";
    Context context;
    publicstaticSharedPreferences sharedPreferences;
    publicstaticSharedPreferences.Editor editor;
    publicstaticString token;

    publicAuthentication() {
        super();
    }

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

    // speichert Token in den Shared PreferencespublicstaticvoidsetToken(String token) {
        Log.d("Cookie", token);
        if(editor==null){
            thrownewNullPointerException("Register your application "+Authentication.class+" in AndroidManifiest.xml");
        }
        editor.putString("Cookie", token);
    }

}

AndroidManifiest.xml

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

Post a Comment for "Save String (cookie) To Sharedprefs Caused Nullpointerexception"