0

我正在使用 AsyncTask 创建进度对话框,然后开始一个新活动。代码是: -

package com.android.grad;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;

public class LoginTask extends AsyncTask<Void, Void, Boolean> {

    private Activity activity;
    private ProgressDialog pd;
    private Uri fileUri;
    private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

    public LoginTask(Activity activity, Uri fileUri) {
        this.activity = activity;
        this.fileUri = fileUri;
    }

    @Override
    protected void onPreExecute() {
        //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        pd = ProgressDialog.show(activity, "Signing in",
                "Please wait while we are signing you in..");
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        pd.dismiss();
        pd = null;
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        activity.startActivityForResult(intent,
                CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }   
}

当我使用这个动作监听器按下按钮时,这个任务就开始了。

private OnClickListener loginOnClick = new OnClickListener() {
        public void onClick(View v) {
            new LoginTask(LoginActivity.this, fileUri).execute();
        }
    };

当方向改变时我得到了那个错误。

06-25 20:41:27.199: E/WindowManager(8867): Activity com.android.grad.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2be69ff8 that was originally added here
06-25 20:41:27.199: E/WindowManager(8867): android.view.WindowLeaked: Activity com.android.grad.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2be69ff8 that was originally added here
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.Dialog.show(Dialog.java:278)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.ProgressDialog.show(ProgressDialog.java:116)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.ProgressDialog.show(ProgressDialog.java:99)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.ProgressDialog.show(ProgressDialog.java:94)
06-25 20:41:27.199: E/WindowManager(8867):  at com.android.grad.LoginTask.onPreExecute(LoginTask.java:25)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.AsyncTask.execute(AsyncTask.java:511)
06-25 20:41:27.199: E/WindowManager(8867):  at com.android.grad.LoginActivity$1.onClick(LoginActivity.java:36)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.View.performClick(View.java:3527)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.View$PerformClick.run(View.java:14234)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.Handler.handleCallback(Handler.java:605)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.Looper.loop(Looper.java:137)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.ActivityThread.main(ActivityThread.java:4441)
06-25 20:41:27.199: E/WindowManager(8867):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 20:41:27.199: E/WindowManager(8867):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 20:41:27.199: E/WindowManager(8867):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 20:41:27.199: E/WindowManager(8867):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 20:41:27.199: E/WindowManager(8867):  at dalvik.system.NativeStart.main(Native Method)
06-25 20:41:27.309: D/dalvikvm(8867): GC_CONCURRENT freed 265K, 4% free 9339K/9671K, paused 6ms+4ms
06-25 20:41:27.519: D/AndroidRuntime(8867): Shutting down VM
06-25 20:41:27.519: W/dalvikvm(8867): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
06-25 20:41:27.529: E/AndroidRuntime(8867): FATAL EXCEPTION: main
06-25 20:41:27.529: E/AndroidRuntime(8867): java.lang.IllegalArgumentException: View not attached to window manager
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:587)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:324)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:151)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.app.Dialog.dismissDialog(Dialog.java:321)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.app.Dialog$1.run(Dialog.java:119)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.os.Handler.handleCallback(Handler.java:605)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.os.Looper.loop(Looper.java:137)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.app.ActivityThread.main(ActivityThread.java:4441)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at java.lang.reflect.Method.invokeNative(Native Method)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at java.lang.reflect.Method.invoke(Method.java:511)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at dalvik.system.NativeStart.main(Native Method)

我在网站上搜索,但没有找到适合我的解决方案

@Override
    protected void onPreExecute() {
        //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        pd = ProgressDialog.show(activity.getApplicationContext(), "Signing in",
                "Please wait while we are signing you in..");
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        pd.dismiss();
        pd = null;
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        activity.startActivityForResult(intent,
                CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }   

06-25 21:23:59.709: E/AndroidRuntime(10041): FATAL EXCEPTION: main
06-25 21:23:59.709: E/AndroidRuntime(10041): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:517)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.Dialog.show(Dialog.java:278)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.ProgressDialog.show(ProgressDialog.java:116)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.ProgressDialog.show(ProgressDialog.java:99)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.ProgressDialog.show(ProgressDialog.java:94)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at com.android.grad.LoginTask.onPreExecute(LoginTask.java:25)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.AsyncTask.execute(AsyncTask.java:511)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at com.android.grad.LoginActivity$1.onClick(LoginActivity.java:38)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.View.performClick(View.java:3527)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.View$PerformClick.run(View.java:14234)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.Handler.handleCallback(Handler.java:605)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.Looper.loop(Looper.java:137)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.ActivityThread.main(ActivityThread.java:4441)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at java.lang.reflect.Method.invokeNative(Native Method)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at java.lang.reflect.Method.invoke(Method.java:511)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

1

检查AsyncTask 是否真的在概念上存在缺陷,或者我只是遗漏了什么?

它提供了对此问题的深入调查,并提供了一个很好的解决方案,说明如何在由设备配置更改(方向更改等)引起的活动重启之间保持正在运行的异步任务。

于 2012-06-25T19:01:30.757 回答
1
 com.android.grad.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2be69ff8 that was originally added here

1-看起来您正在使用 Activity 引用来显示对话框,而不是将 Activity cto 构造函数传递pass Application context给它以显示对话框......

2- pd.dismiss(); pd = null; 应该在 doInBackground 中的 onPostExecute 中进行检查,例如 isShowing 或任何更好的检查............

 If(null!=pd && pd.isShowing ()){
      pd.dismiss();

       pd = null;
    }
于 2012-06-25T18:49:07.303 回答
1

在旋转时,Activity 被销毁,您的 ProgressDialog 也是如此,因此,当您尝试隐藏对话框时,它不再存在并生成此异常。

于 2012-06-25T18:47:11.330 回答