异步任务下载,如果没有连接抛出空指针异常

时间:2012-07-27 08:36:50

标签: android multithreading download android-asynctask nullpointerexception

先生,如果没有互联网连接,我正试图处理这条消息。但是,如果有任何网络连接我仍然继续下载,则会发生null异常错误。首先,它显示网络连接中的错误消息,但是当我按yes时,弹出异常错误消息。当我们处理这个案件时,我们应该进一步处理什么?

   timer = new Timer();
    boolean isOnline = WebServiceHandler.isOnline(this);
    if (isOnline) {
        startDownloadTask();
     task = new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                int progress = downloadProgressBar.getProgress();
                notification.contentView.setProgressBar(R.id.pb, downloadProgressBar.getMax(), progress, false);
                nm.notify(notificationID, notification);
            }
        };

        timer.schedule(task, 10, 500);

    } else {
        connectionErrorDialog.show();
        onDestroy();


    }

}    




@Override
public void stopDownloadTask() {
    downloadGoodListTask.cancel(true);
    downloadGoodListTask.setActivityIsRunning(false);
    downloadGoodListTask = null;

}


 protected void onDestroy() {
    super.onDestroy();
    Log.i("Download Activity", "starting to destroy");
    //timer.cancel();


    wheelProgressBar.destroyDrawingCache();
    downloadProgressBar.destroyDrawingCache();
    finishDialog.dismiss();
    connectionErrorDialog.dismiss();

    downloadProgressBar = null; 
    wheelProgressBar = null;
    progressTextView = null;
    downloadingTextView = null;
    finishDialog = null;
    connectionErrorDialog = null;        
    //timer = null;     
    nm.cancel(notificationID);

    nm = null;
    notification = null;
    /*downloadTask.cancel(true);
    downloadTask.activityIsRunning = false;
    downloadTask = null;*/
    stopDownloadTask();
    timer.cancel();        
    System.gc();

    Log.i("Download Activity", "destroyed");
}


@Override
public void stopDownloadTask() {
    downloadGoodListTask.cancel(true);
    downloadGoodListTask.setActivityIsRunning(false);
    downloadGoodListTask = null;

}

2 个答案:

答案 0 :(得分:2)

不要从UI线程外部访问Android UI工具包。使用runOnUIThread方法,向Handler发送消息或使用AsyncTask。

答案 1 :(得分:0)

异常是因为,downloadGoodListTask为null。或者,您应该初始化为某个值或在stopDownloadTask()中处理异常。