处理屏幕方向

时间:2012-06-25 18:45:46

标签: android android-asynctask

我正在使用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);
    }   
}

当我使用此Action侦听器按下按钮时,此任务开始。

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)

3 个答案:

答案 0 :(得分:1)

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

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

答案 2 :(得分:1)

检查Is AsyncTask really conceptually flawed or am I just missing something?

它提供了对此问题的深入研究,以及如何在设备配置更改(方向更改等)导致的活动结果之间保持运行异步任务的良好解决方案。