在Android中尝试取消AsyncTask中的进度对话框时出现空指针异常

时间:2011-12-26 18:39:17

标签: android dialog android-asynctask

我在取消异步任务中的进度对话框时获得空指针异常。这是我的代码..我从RSS Conecction获取数据,为期7天。我想取消对话框和如果用户按下后退按钮,则停止任务。

          @Override
    protected void onPreExecute() {
        showDialog(DIALOG_PROGRESS);
        Calendar calendar = null;
        switch (day) {
            case SAT:
                calendar = DateUtil.getSaturdayDate();
                break;
            case SUN:
                calendar = DateUtil.getSundayDate();
                break;
            case MON:
                calendar = DateUtil.getMondayDate();
                break;
            case TUE:
                calendar = DateUtil.getTuesdayDate();
                break;
            case WED:
                calendar = DateUtil.getWednesdayDate();
                break;
            case THU:
                calendar = DateUtil.getThursdayDate();
                break;
            case FRI:
                calendar = DateUtil.getFridayDate();
                break;
            default:
                break;
        }

        if(calendar != null) {
            generatedLink = WeeklyScheduleBean.generatePath( DateUtil.getDay(calendar),
                                                             DateUtil.getMonth(calendar),
                                                             DateUtil.getYear(calendar) );
        } else {
            // get the schedule of today
            Calendar current_day = DateUtil.getCurrentTime();
            generatedLink = WeeklyScheduleBean.generatePath( DateUtil.getDay(current_day),
                                                             DateUtil.getMonth(current_day),
                                                             DateUtil.getYear(current_day) );
        }
    }

        @Override
    protected Boolean doInBackground(Void... params) {
        feedParser = new WeeklyScheduleParser(generatedLink);
        list = new ArrayList<WeeklyScheduleBean>();
        try {
            list = feedParser.parse();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }           

        if(list.size() > 0)                             
            return true;
        else 
            return false;           
    }



        @Override
    protected void onPostExecute(Boolean result) {
        if(progressDialog.isShowing()){
        if(result) {
            weeklyScheduleScreen.removeDialog(DIALOG_PROGRESS);
            adapter = new WeeklyAdapter(cxt, R.layout.time_text_list, R.id.title_text, list);
            lv.setAdapter(adapter);
        } 
        else {
            DialogUtil.CreateDialog("", "Could not retreive the feeds",false,WeeklyScheduleScreen.this);
            weeklyScheduleScreen.removeDialog(DIALOG_PROGRESS);
        }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

尝试将showDialog(DIALOG_PROGRESS);更改为progressDialog.show();

答案 1 :(得分:0)

您没有在任何地方初始化weeklyScheduleScreen。此外,由于可以在没有任何对象的情况下调用showDialog,我的猜测是这个AsyncTask嵌套在您的活动类中。在这种情况下,您不需要weeklyScheduleScreen。只需将其更改为removeDialog(DIALOG_PROGRESS);(不含weeklyScheduleScreen.)。

答案 2 :(得分:0)

你应该检查:`

if (isCancelled())
在您的AsyncTask方法中

,您应该考虑覆盖onCancelled方法 AsyncTask在适当的时候关闭你的进度对话框。