应用程序崩溃启动 - 空指针

时间:2013-12-19 02:56:21

标签: android android-activity android-asynctask nullpointerexception

我从一个恢复的应用程序中获取此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()

中没有任何内容

FragmentAsyncTask一直处于后台时调用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再次被调用)时,这只会崩溃。

2 个答案:

答案 0 :(得分:2)

执行时会抛出

NullPointerExceptionDebtPlanner.PlanType.equals("highint")。有两个可能的原因:

(1)DebtPlannernull,或(2)PlanTypenull

解决方案验证这两个字段是否已初始化,并且在执行此行时不为空。这应该可以解决问题。

答案 1 :(得分:0)

对于更长时间的操作,

LoaderManager和AsyncTaskLoader优于AsyncTask。