mohamedrashad mohamedrashad - 1 month ago 8
Android Question

Cannot save values in sharedpreferences using ASyncTask

I'm developing a twitter client, I can log in and get access token successfully and display it, I want to save the token in shared preferences and use them later (to not make the user login every time)
i use a AsyncTask to get token, on postExecute I put Shared preferences but it force closes.

this is my Async:

private class GetAccessTokenTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
String verifier = strings[0];
try {
accessToken = twitter.getOAuthAccessToken(verifier);
Log.d(MainActivity.class.getSimpleName(), accessToken.getToken());

} catch (Exception e) {

Log.d(MainActivity.class.getSimpleName(),"nooooooooooooooooooooooo");

}

return null;
}

@Override
protected void onPostExecute(Void result){

Editor editor = mSharedPreferences.edit();
editor.putString(ACCESS_TOKEN , accessToken.getToken());
editor.putString(ACCESS_SECRET, accessToken.getTokenSecret() );
editor.commit();

Toast.makeText(getApplicationContext(), "hola", 54).show();
super.onPostExecute(result);
}
}


logcat is here:

05-26 16:52:27.843: E/AndroidRuntime(7948): FATAL EXCEPTION: main
05-26 16:52:27.843: E/AndroidRuntime(7948): java.lang.NullPointerException
05-26 16:52:27.843: E/AndroidRuntime(7948): at tk.pidevelopers.floata.MainActivity$GetAccessTokenTask.onPostExecute(MainActivity.java:168)
05-26 16:52:27.843: E/AndroidRuntime(7948): at tk.pidevelopers.floata.MainActivity$GetAccessTokenTask.onPostExecute(MainActivity.java:1)
05-26 16:52:27.843: E/AndroidRuntime(7948): at android.os.AsyncTask.finish(AsyncTask.java:417)
05-26 16:52:27.843: E/AndroidRuntime(7948): at android.os.AsyncTask.access$300(AsyncTask.java:127)
05-26 16:52:27.843: E/AndroidRuntime(7948): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
05-26 16:52:27.843: E/AndroidRuntime(7948): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 16:52:27.843: E/AndroidRuntime(7948): at android.os.Looper.loop(Looper.java:130)
05-26 16:52:27.843: E/AndroidRuntime(7948): at android.app.ActivityThread.main(ActivityThread.java:3835)
05-26 16:52:27.843: E/AndroidRuntime(7948): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 16:52:27.843: E/AndroidRuntime(7948): at java.lang.reflect.Method.invoke(Method.java:507)
05-26 16:52:27.843: E/AndroidRuntime(7948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
05-26 16:52:27.843: E/AndroidRuntime(7948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
05-26 16:52:27.843: E/AndroidRuntime(7948): at dalvik.system.NativeStart.main(Native Method)

Answer Source

Seems like mSharedPreferences is null. You can override onPreExecute method of AsynkTask and create SharedPreferences object mSahredPreferences or you can do the same in onPostExecute.

@Override
protected void onPostExecute(Void result){
     mSharedPreferences = getSharedPreferences(<Name>, <Mode>);
     Editor editor = mSharedPreferences.edit();
     editor.putString(ACCESS_TOKEN , accessToken.getToken());
     editor.putString(ACCESS_SECRET, accessToken.getTokenSecret() ); 
     editor.commit();

     Toast.makeText(getApplicationContext(), "hola", 54).show();
         super.onPostExecute(result);
}

Please note that you need to call getSharedPreferences on Context if you are not doing stuffs in Activity.