我从一个恢复的应用程序中获取此LogCat(在Activity
已经过去之后(不是每次都有)。它实际上是一个特定的Fragment
,所以当FragmentActivity
时恢复。
12-18 18:45:33.315: E/AndroidRuntime(7593): FATAL EXCEPTION: AsyncTask #1
12-18 18:45:33.315: E/AndroidRuntime(7593): java.lang.RuntimeException: An error occured while executing doInBackground()
12-18 18:45:33.315: E/AndroidRuntime(7593): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-18 18:45:33.315: E/AndroidRuntime(7593): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-18 18:45:33.315: E/AndroidRuntime(7593): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-18 18:45:33.315: E/AndroidRuntime(7593): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-18 18:45:33.315: E/AndroidRuntime(7593): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-18 18:45:33.315: E/AndroidRuntime(7593): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-18 18:45:33.315: E/AndroidRuntime(7593): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-18 18:45:33.315: E/AndroidRuntime(7593): at java.lang.Thread.run(Thread.java:856)
12-18 18:45:33.315: E/AndroidRuntime(7593): Caused by: java.lang.NullPointerException
12-18 18:45:33.315: E/AndroidRuntime(7593): at com.---.---.DebtDataSource.payoffDebt(DebtDataSource.java:229)
12-18 18:45:33.315: E/AndroidRuntime(7593): at com.---.---.PlannerFragment$PlannerTask.doInBackground(PlannerFragment.java:131)
12-18 18:45:33.315: E/AndroidRuntime(7593): at com.---.---.PlannerFragment$PlannerTask.doInBackground(PlannerFragment.java:1)
12-18 18:45:33.315: E/AndroidRuntime(7593): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-18 18:45:33.315: E/AndroidRuntime(7593): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-18 18:45:33.315: E/AndroidRuntime(7593): ... 4 more
主要活动有:
@Override
protected void onDestroy() {
super.onDestroy();
datasource.close();
finish();
}
onResume()
。
Fragment
在AsyncTask
一直处于后台时调用Activity
。
问题是DoInBackground
基于日志。 (PlannerFragment
)
@Override
protected Void doInBackground(String... params) {
if (paymentTotal > allFee) {
totalMonths = datasource.payoffDebt(Double.valueOf(columnDebtTotal)); // this is line 131
finalDate = datasource.getDate(totalMonths);
allFeeLife = datasource.getLiftimeFees();
}
return null;
}
然后Nullpointer在Nullpointer尝试从我的SQLite DB中获取数据之后。
(DebtDataSource
Class)
public int payoffDebt(Double totalDebt) {
Cursor c = null;
if (DebtPlanner.PlanType.equals("highint")) { // line 229 -- NULL CRASH!
c = database.rawQuery("SELECT * FROM debt ORDER BY CAST("
+ MySQLiteHelper.COLUMN_APR + " as integer) DESC;", null);
}
同样,只有当Activity
在后台很长一段时间后恢复(足以让task
中的onActivityCreated
再次被调用)时,这只会崩溃。
答案 0 :(得分:2)
NullPointerException
:DebtPlanner.PlanType.equals("highint")
。有两个可能的原因:
(1)DebtPlanner
为null
,或(2)PlanType
为null
解决方案验证这两个字段是否已初始化,并且在执行此行时不为空。这应该可以解决问题。
答案 1 :(得分:0)
LoaderManager和AsyncTaskLoader优于AsyncTask。