ProgressDialog仅在纵向模式下泄露窗口错误

时间:2013-12-09 05:55:31

标签: android progressdialog android-orientation

我正在开发一款可以拍照并进行长时间处理的应用。在处理过程中我想通过ProgressDialog显示状态,因此我将AsyncTask类扩展为如下

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 。 我在这里缺少什么?

* 请注意,即使没有方向更改,也会发生此错误

发生错误时的LogCat

12-09 21:34:57.080: E/WindowManager(15365):     Activity com.example.jaundicedetect.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@410f5070 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 com.android.internal.policy.impl.PhoneWindow$DecorView@410f5070 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)

3 个答案:

答案 0 :(得分:0)

试试这个 -

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

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

答案 1 :(得分: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();
            }
        });
    }
    }

尝试使用这样的方法。 将它用作实用工具类。

答案 2 :(得分:0)

很抱歉,显然错误发生在ResultTask实例之前的几行中,我是从multiCrop视图中提取位图,这是在视图范围之外。

感谢你的所有建议。