0

为什么我得到这个空指针异常。这是我的代码,在我第一次从 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;
            }
        }

我错过了什么?

4

2 回答 2

0

如下所述修改您的 onPreExcecute 方法:

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

于 2015-02-26T07:08:09.000 回答
0

mProgressDialog你的似乎ScanVideoTask从未初始化过。你在哪里启动你的ScanVideoTask

于 2012-06-06T21:33:07.967 回答