【发布时间】:2013-04-06 19:50:55
【问题描述】:
当我尝试使用从 doInBackground 收到的值更新 SharedPreferences 时,AsyncTask onPostExecute 崩溃(报告 nullpointerexecption)。
我对调用活动的处理是:private DashboardActivity<?> callerActivity; 没有它抱怨的<?>。我尝试了几种方法:1)将 onPostExecute 中的 SP 更新为:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(callerActivity);
prefs.edit().putInt("lastrecord", intRec ).commit();
我还在 callerActivity 中调用了一个方法:
callerActivity.storeLastInsertedRecord(intRec);
所有的错误都是一样的,nullpointerexecution 就在这几行。
我可能做错了什么? @denisdrew 的这个答案Android - shared preferences set in an AsyncTask onPostExecute() not always set? 是最接近的,但是当我尝试实例化意图时它在同一个地方崩溃:
Intent myIntent = new Intent(callerActivity.getApplicationContext(), SharedPrefsHelperActivity.class);
或
Intent myIntent = new Intent(callerActivity.getBaseContext(), SharedPrefsHelperActivity.class);
我可能做错了什么?我知道我要推送的值不是 null (intRec)。
【问题讨论】:
-
为什么 DashboardActivity 是通用的?
-
部分是因为我不知道自己在做什么,并且在 Eclipse 建议的选项中,我采用了通用选项。当我将其保留为
DashboardActivity callerActivity;时,其他通过 json 与我的 mysql 交互的 AsyncTask 类不会抱怨
标签: android android-asynctask sharedpreferences