2012-06-06 68 views
0

爲什麼我得到這個空指針異常。這裏是我的代碼 我正在逐漸OnProgressUpdate方法異常從doInBackground方法當Android進度對話框中的setProgress爲空指針異常

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

這裏我publishProgress的第一次執行後,在我的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; 
      } 
     } 

我缺少的是什麼?

+0

請添加一個堆棧跟蹤。 –

+0

難道你沒有將DIALOG_DOWNLOAD_PROGRESS傳遞給你的OnCreate方法嗎?這將解釋爲什麼它沒有被初始化。 – Guardanis

回答

0

您的ScanVideoTaskmProgressDialog似乎從未初始化。 你在哪裏啓動你的ScanVideoTask

+0

我的不好。只是沒有意識到。謝謝 – user973743

0

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

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