0

谁能帮我解决这个问题。在这里,我从服务中下载了数据,为此我使用了后台服务并显示了进度对话框。它在我的手机上完美运行,但相同的代码在我的平板电脑中出现问题:

这是我的代码:

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)

有人可以帮忙吗?

提前致谢。

4

3 回答 3

2

你可以试试这个。有时问题可能是因为进度对话框可能没有正确初始化。不要在后台做任何与 UI 相关的事情,即使尝试在 runonUIthread 中进行操作,否则可能会发生 windowleak

 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

            }
    }
于 2013-07-12T10:56:15.050 回答
0

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

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

您确定HomeActivity应该显示进度拨号盘时没有弯腰吗?

于 2013-07-12T10:49:09.850 回答
0

嗨,问题是关于 ProgressDialog(),它在活动切换期间没有隐藏(再次重新打开进度条的相同活动)。

尝试在 onPause() 和 show() 中隐藏进度对话框,在 onResume() 上相同。

于 2013-07-12T11:08:11.857 回答