在android进度对话框中setProgress时出现空指针异常

时间:2012-06-06 21:22:10

标签: android progressdialog

为什么我得到这个空指针异常。这是我的代码 我从doInBackground方法首次执行publishProgress后,在OnProgressUpdate方法中获取异常

private class ScanVideoTask extends AsyncTask<String, Integer, String> {
        private AsyncTaskCompleteListener<String> callback;
        private Context context;
        private String resultOutput;
        private ProgressDialog mProgressDialog;

        public ScanVideoTask(AsyncTaskCompleteListener<String> cb) {
            this.callback = cb;
        }

        protected String doInBackground(String... args) {
            // Get the URI of the video path & display it for a short period.
            String filename = args[0];

            int i= 0;
            while(i < 1000000)
            {
                i++;
                int progressPercentage = (int)(((float)i/(float)1000000) * (float)100);
                publishProgress(progressPercentage);
            }
            return "ok";

         }

         protected void onProgressUpdate(Integer... progress) {
             mProgressDialog.setProgress(progress[0]);
         }

         protected void onPreExecute() {
             super.onPreExecute();
             showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }

         protected void onPostExecute(String result) {
            System.out.println("on Post execute called" + result);
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            callback.onTaskComplete(result);
         }
     }

这是我的onCreateDialog

中的内容
 @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_DOWNLOAD_PROGRESS:
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setMessage("Scanning video..");
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mProgressDialog.setCancelable(true);
                mProgressDialog.show();
                return mProgressDialog;
            default:
                return null;
            }
        }

我缺少什么?

2 个答案:

答案 0 :(得分:0)

mProgressDialog的{​​{1}}似乎从未初始化。 你在哪里推出ScanVideoTask

答案 1 :(得分:0)

修改你的onPreExcecute方法,如下所述:

&#13;
&#13;
protected void onPreExecute() 
{
super.onPreExecute();
mProgressDialog = new ProgressDialog(this);
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
&#13;
&#13;
&#13;