BackgroundAsyncTask在Tablet中出错

时间:2013-07-12 10:43:30

标签: android dialog android-asynctask

任何人都可以帮我解决这个问题。在这里,我从服务中下载了数据,因为我使用了后台服务并显示了进度对话框。它完全在我的手机上工作,但相同的代码在我的平板电脑中出现问题:

这是Mycode:

public class BackgroundAsyncTaskForFirstTimeHitOrCacheExpired extends AsyncTask<String, Void, Boolean> {
        public ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(HomeActivity.this, "", "Downloading...", true);
        }

        @Override
        protected Boolean doInBackground(String... arg0) {

                    //Download data         
                }       

        @Override
        protected void onPostExecute(Boolean isServiceValuesFetchedSuccessfully) {
            dialog.dismiss();
            //activity started

        }
    }

这是我的logcat错误:

    07-12 16:31:31.597: E/AndroidRuntime(2493): FATAL EXCEPTION: main
07-12 16:31:31.597: E/AndroidRuntime(2493): java.lang.IllegalArgumentException: View not attached to window manager
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:385)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:230)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.view.Window$LocalWindowManager.removeView(Window.java:432)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.app.Dialog.dismissDialog(Dialog.java:290)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.app.Dialog.access$000(Dialog.java:78)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.app.Dialog$1.run(Dialog.java:123)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.app.Dialog.dismiss(Dialog.java:280)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at com.cpt.realtor.activity.HomeActivity$BackgroundAsyncTaskForFirstTimeHitOrCacheExpired.onPostExecute(HomeActivity.java:150)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at com.cpt.realtor.activity.HomeActivity$BackgroundAsyncTaskForFirstTimeHitOrCacheExpired.onPostExecute(HomeActivity.java:1)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.os.AsyncTask.finish(AsyncTask.java:417)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.os.Looper.loop(Looper.java:123)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at android.app.ActivityThread.main(ActivityThread.java:3687)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at java.lang.reflect.Method.invokeNative(Native Method)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at java.lang.reflect.Method.invoke(Method.java:507)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-12 16:31:31.597: E/AndroidRuntime(2493):     at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮忙吗?

提前致谢。

3 个答案:

答案 0 :(得分:2)

你可以尝试一下。有时问题可能是因为进展稀释可能没有正确初始化。在背景中不要做与UI相关的任何事情,即使做某事尝试在runonUIthread中做出外向窗口漏洞也可能发生

 public class BackgroundAsyncTaskForFirstTimeHitOrCacheExpired extends AsyncTask<String, Void, Boolean> {
            private ProgressDialog dialog;
            @Override
            protected void onPreExecute() {
                dialog = new ProgressDialog(DictateActivity.this);
                dialog.setCancelable(false);
                dialog.setMessage("Downloadin..");
                dialog.show();
            }

            @Override
            protected Boolean doInBackground(String... arg0) {

                        //Download data         
                    }       

            @Override
            protected void onPostExecute(Boolean isServiceValuesFetchedSuccessfully) {

                 dialog.dismiss();
                //activity started

            }
    }

答案 1 :(得分:0)

可能是您将错误的上下文传递给进度对话框

dialog = ProgressDialog.show(HomeActivity.this, "", "Downloading...", true);

在显示进度拨号时,您确定HomeActivity没有被加入吗?

答案 2 :(得分:0)

问题是关于ProgressDialog(),它在活动切换期间未被隐藏(同一活动再次重新打开进度条)。

尝试在onPause()中隐藏进度对话框,并在onResume()上显示()。