2013-12-09 54 views
0

我正在開發一個應用程序,它會拍攝一張照片並進行一些長時間的處理。在處理期間,我想顯示經由ProgressDialog的狀態,因此我延長的AsyncTask類作爲如下所示ProgressDialog只在肖像模式下泄漏窗口錯誤

以下

private class ResultTask extends AsyncTask<multiCrop, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = new ProgressDialog(MainActivity.this); 
     progressDialog.setTitle("Analyzing, please wait ..."); 
     progressDialog.setMessage("Loading.."); 
     progressDialog.setCancelable(false); 
     progressDialog.show(); 
    } 

    @Override 
    protected String doInBackground(multiCrop... mcImgV) { 
     String result = null; 
     //Long processing that uses view multiCrop and occasional publishProgress() calls, returns String result 
    } 

    @Override 
    protected void onProgressUpdate(String... str) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(str); 
     progressDialog.setMessage(str[0]); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     ((TextView)findViewById(R.id.resultText)).append(result); 
     progressDialog.dismiss(); 
    } 

} 

ResultTask一個實例在視圖中的按鈕之一的的onClick處理程序創建的

public void onResultClick(View v){ 
    ResultTask task = new ResultTask(); 
    task.execute(babyImageView); 
} 

當設備處於景觀代碼工作沒有任何問題,但如果onResultClick事件在肖像模式觸發我得到一個錯誤,指出MainActivity有LeakedWindow。 我在這裏錯過了什麼?

*請注意,即使沒有方向變化時出現錯誤

12-09 21:34:57.080: E/WindowManager(15365):  Activity com.example.jaundicedetect.MainActivity has leaked window [email protected] that was originally added here 
12-09 21:34:57.080: E/WindowManager(15365):  android.view.WindowLeaked: Activity com.example.jaundicedetect.MainActivity has leaked window [email protected] that was originally added here 
12-09 21:34:57.080: E/WindowManager(15365):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:350) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.view.Window$LocalWindowManager.addView(Window.java:537) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.app.Dialog.show(Dialog.java:286) 
12-09 21:34:57.080: E/WindowManager(15365):  at com.example.jaundicedetect.MainActivity$ResultTask.onPreExecute(MainActivity.java:328) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.os.AsyncTask.execute(AsyncTask.java:511) 
12-09 21:34:57.080: E/WindowManager(15365):  at com.example.jaundicedetect.MainActivity.onResultClick(MainActivity.java:524) 
12-09 21:34:57.080: E/WindowManager(15365):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-09 21:34:57.080: E/WindowManager(15365):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.view.View$1.onClick(View.java:3039) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.view.View.performClick(View.java:3511) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.view.View$PerformClick.run(View.java:14110) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.os.Handler.handleCallback(Handler.java:605) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.os.Handler.dispatchMessage(Handler.java:92) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.os.Looper.loop(Looper.java:137) 
12-09 21:34:57.080: E/WindowManager(15365):  at android.app.ActivityThread.main(ActivityThread.java:4447) 
12-09 21:34:57.080: E/WindowManager(15365):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-09 21:34:57.080: E/WindowManager(15365):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-09 21:34:57.080: E/WindowManager(15365):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
12-09 21:34:57.080: E/WindowManager(15365):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
12-09 21:34:57.080: E/WindowManager(15365):  at dalvik.system.NativeStart.main(Native Method) 
+1

從您的logcat發佈錯誤。 –

+0

希望這將幫助你'http:// stackoverflow.com/questions/1111980/how-to-handle-screen-orientation-change-when-progress-dialog-and-background-thre' –

+0

Show full logcat –

回答

0

試試這個

logcat中出現此錯誤 -

protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
    progressDialog.dismiss(); 

    ((TextView)findViewById(R.id.resultText)).append(result); 
     } 
+0

依然存在的問題仍然存在 – jMathew

0

第一件事嘗試在UI線程內使用進度對話框。

 public class LoginScreen extends FragmentActivity { 
     ProgressDialog progressDialog; 


     @Override 
     protected void onCreate(Bundle arg0) { 
      // TODO Auto-generated method stub 
      super.onCreate(arg0); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 

       setContentView(R.layout.activity_login); 
    progressDialog = ProgressDialog.show(
               LoginScreen.this, null, 
               "Logging you in..."); 
             progressDialog.setCancelable(false); 
    } 
public void progressDialogDismiss() { 
     LoginScreen.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       if (progressDialog.isShowing()) 
        progressDialog.dismiss(); 
      } 
     }); 
    } 
    } 

試着用這樣的。 使用它一個工具類。

0

對不起人,顯然錯誤是在ResultTask實例之前的幾行中,我從位於視圖邊界之外的multiCrop視圖中提取位圖。

感謝您的建議。