无法使用ASyncTask在共享首选项中保存值

时间:2014-05-26 13:25:00

标签: android twitter android-asynctask sharedpreferences

我正在开发一个Twitter客户端,我可以登录并成功获取访问令牌并显示它,我想将令牌保存在共享首选项中并稍后使用它们(不要每次都让用户登录) 我使用AsyncTask获取令牌,在postExecute上我放置了共享偏好但强行关闭。

这是我的异步:

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在这里:

  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)

1 个答案:

答案 0 :(得分:0)

似乎mSharedPreferencesnull。您可以覆盖onPreExecute的{​​{1}}方法并创建AsynkTask对象SharedPreferences,也可以在mSahredPreferences中执行相同操作。

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); } 中的内容,则需要致电getSharedPreferences Context