Null Pointer exception when trying to cancel Progress Dialog in AsyncTask in android

寵の児 提交于 2019-12-11 13:17:10

问题


I'm Getting a Null Pointer Exception while canceling the Progress Dialog in Async Task ..this is my code..I'm Getting Data from an RSS Conecction for 7 days of Week..I want to cancel the Dialog and stop the task if the user pressed Back Button.

          @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);
        }
        }
    }
}

回答1:


try to change showDialog(DIALOG_PROGRESS); to progressDialog.show();




回答2:


You are not initializing weeklyScheduleScreen anywhere. Also, since showDialog can be called without any object, my guess is that this AsyncTask is nested within your activity class. In that case, you don't need weeklyScheduleScreen. Just change it to removeDialog(DIALOG_PROGRESS); (without the weeklyScheduleScreen.).




回答3:


You should check : `

if (isCancelled())

within your AsyncTask methods and you should think about overriding the onCancelled method of AsyncTask to dismiss your progress dialog at the right time.



来源:https://stackoverflow.com/questions/8638265/null-pointer-exception-when-trying-to-cancel-progress-dialog-in-asynctask-in-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!