我正在開發一個應用程序,它會拍攝一張照片並進行一些長時間的處理。在處理期間,我想顯示經由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)
從您的logcat發佈錯誤。 –
希望這將幫助你'http:// stackoverflow.com/questions/1111980/how-to-handle-screen-orientation-change-when-progress-dialog-and-background-thre' –
Show full logcat –