错误在AsyncTask的解雇ProgressDialog [英] Error on dismissing ProgressDialog in AsyncTask

查看:232
本文介绍了错误在AsyncTask的解雇ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完全独立的类,它扩展了AsyncTask的,我用它来执行一个HTML的GET,所以它不会冻结UI。在AsyncTask的Im的构造函数传递活动的背景下,这将调用任务的.execute(),所以htmlget运行时,我可以显示一个ProgressDialog。来到这里的第一个问题:旋转手机时,当前视图得到破坏,当应用程序达到对话框的.dismiss(),它崩溃,因为它连接到已经不存在的观点。我还是设法解决这个问题的情况下,像95%,但是在剩下的5%,但仍是一​​个问题。我原来的解决方案是强制禁用的循环,而htmlget运行时,通过传递活动到AsyncTask的为好,并调用

I have a completely separate class, which extends AsyncTask, I use it for executing a html get, so it wont freeze the UI. In the constructor of the AsyncTask Im passing the activity's context, which invokes the .execute() of the task, so I can show a ProgressDialog while the htmlget is running. Here came the first problem: when rotating the phone, the current view got destroyed, and when the app reached the .dismiss() of the dialog, it crashed, since the view it was attached to does not exist anymore. I did manage to solve this issue in like 95% of the cases, however in the remaining 5%, there is still a problem. My original solution was to force disable the rotation, while the htmlget is running, by passing the activity to the AsyncTask as well, and invoking

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

在上preExecute(),并调用

in the onPreExecute(), and invoking

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

在onPostExecute(),即使用户旋转屏幕这样,它不会破坏当前活动。但是,正如我前面提到的,在剩下的5%,但仍然是一个问题,这将导致强制关闭。我所知道的有以下几种:

in the onPostExecute(), this way even if the user rotates the screen, it won't destroy the current activity. But, as I already mentioned, in the remaining 5%, there is still a problem, which causes force close. All I know are the following:

我有这个堆栈跟踪,这我的用户通过Android电子市场提供的我:

I have this StackTrace, which my users supplied me via Android Market:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:381)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:226)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.app.Dialog.dismiss(Dialog.java:268)
at com.vasga.telvira.AsyncHttpGetter.onPostExecute(AsyncHttpGetter.java:160)
at com.vasga.telvira.AsyncHttpGetter.onPostExecute(AsyncHttpGetter.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)

它发生在AsyncTask的,以下行(这是在堆栈跟踪中提到的第160行):

it happens in the AsyncTask, on the following line(this is the 160th line mentioned in the StackTrace):

if(pd.isShowing()) pd.dismiss(); //dismiss the pd after we are done, if it is visible(it should be)

在pd.isShowing()不能引起这一点,因为我补充说,如果作为第一个可能的解决方案,也许对话框中没有显示在某些罕见的情况,但没有成功。

the pd.isShowing() cannot cause this, since I added that if clause as a possible solution, maybe the dialog wasn't showing on some rare occasions, but it did not work out

我的应用程序与Android 1.6.x的兼容,但有一个编译级设置好的在Eclipse中的Andr​​oid 2.2.x的,因为我已经启用了移动到SD卡功能

my application is compatible with android 1.6.x, but have a Compiling level setted for android 2.2.x in eclipse, since I have the "move to sd card" feature enabled

判断来自一些用户发送的消息后,便开始了htmlget,他们极有可能看到ProgressDialog雨后春笋般冒出来,然后应用程序崩溃。

judging from the few user sent messages, they start the htmlget, they most likely see the ProgressDialog popping up, and then the application crashes

任何想法是什么原因?或如何解决呢?是我NOSENSOR的做法是否正确?

Any idea what can cause this? Or how to solve it? Is my NOSENSOR approach correct?

推荐答案

似乎错误已经走了,至少我没有接到在新版本中的任何新的报告。我所做的,我的主要活动overrided这样的:

it seems that the error has gone, at least I did not received any new reports in the new version. What I did was, I overrided this in the main activity:

public Dialog onCreateDialog(int id)

然后,我通过活动,任务,设置好的了nosensor的thingie开头,然后当我表现出的对话框中,这样做:

then, I passed the activity to the task, and setted the nosensor thingie at the beginning, then when I showed the dialog, did this:

activity.showDialog(0);

然后,当我不得不解雇了,我这样做:

then, when I had to dismiss it, I did this:

activity.dismissDialog(0);

这似乎是这样工作的。请原谅我,如果有任何的SyntaxError,我不是我的开发计算机上:)

It seems it works this way. Pardon me if there is any syntaxerror, Im not on my developer computer :)

这篇关于错误在AsyncTask的解雇ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆