由 AsyncTask 调用时 ProgressDialog.dimiss 方法出错
Posted
技术标签:
【中文标题】由 AsyncTask 调用时 ProgressDialog.dimiss 方法出错【英文标题】:Error in the ProgressDialog.dimiss method when called by AsyncTask 【发布时间】:2014-01-09 03:11:56 【问题描述】:我使用AsyncTask
从服务器获取数据来为我的listview
构建适配器。但是,如果我在 AsyncTask
中使用 ProgressDialog
,则会出现错误。
代码:
文件 AsyncTaskSetAdapterListViewDetail.java
public class AsyncTaskSetAdapterListViewDetail extends AsyncTask<Void, Void, List<Map<String, Object>>>
private final String query;
private final Context ctx;
private final ProgressDialog Loading;
public final HashMap<String, Object> Data;
private final View listv;
public AsyncTaskSetAdapterListViewDetail(final String query, final Context ctx, View listv, HashMap<String, Object> Data)
this.query = query;
this.ctx = ctx;
this.Loading = new ProgressDialog(ctx);
this.listv = listv;
this.Data = Data;
@Override
protected void onPreExecute()
super.onPreExecute();
Loading.setMessage("Loading...");
Loading.setCancelable(false);
Loading.show();
@Override
protected List<Map<String, Object>> doInBackground(final Void... voids)
List<Map<String, Object>> BrightCoveData = new ArrayList<Map<String, Object>>();
BrightCoveClient bcClient = new BrightCoveClient();
String QueryType = (String) Data.get("QueryType");
if(QueryType == "bccategory")
BrightCoveData = bcClient.QueryData(query, ctx);
else if(QueryType == "bcplaylist")
BrightCoveData = bcClient.QueryDataPlaylist(query, ctx);
return BrightCoveData;
@Override
protected void onPostExecute(List<Map<String, Object>> result)
super.onPostExecute(result);
String[] fromStr = (String[]) Data.get("StringFrom");
int[] toInt = (int[]) Data.get("ToInt");
String strClass = listv.getClass().toString();
String[] typeView = strClass.split("\\.");
int resource = (Integer) Data.get("Resource");
String type = typeView[typeView.length - 1];
for (Map<String, Object> map : result)
String videoID = map.get("videoID").toString();
String name = map.get("title").toString();
String description = map.get("description").toString();
String duration = map.get("duration").toString();
if (!VideoFunctions.isVideoExisted(videoID))
VideoFunctions.addVideo(videoID, name, "", description, duration, "");
ListAdapter adapter = new OoyalaVideoListAdapter(ctx, result, resource, fromStr,toInt);
if ( type.equalsIgnoreCase("ListView"))
((ListView) listv).setAdapter(adapter);
else
((AbsSpinner) listv).setAdapter((SpinnerAdapter) adapter);
if (Loading.isShowing())
Loading.dismiss();
错误:
12-20 16:04:15.347: E/androidRuntime(13344):
java.lang.IllegalArgumentException:
View=com.android.internal.policy.impl.PhoneWindow$DecorView422262a8
V.E..... R.....I. 0,0-0,0 not attached to window manager 12-20
16:04:15.347: E/AndroidRuntime(13344): at
android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
12-20 16:04:15.347: E/AndroidRuntime(13344): at
android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
12-20 16:04:15.347: E/AndroidRuntime(13344): at
android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
12-20 16:04:15.347: E/AndroidRuntime(13344): at
android.app.Dialog.dismissDialog(Dialog.java:329) 12-20 16:04:15.347:
E/AndroidRuntime(13344): at
android.app.Dialog.dismiss(Dialog.java:312) 12-20 16:04:15.347:
E/AndroidRuntime(13344): at
RanhTV.Utils.AsyncTaskSetAdapterListView.onPostExecute(AsyncTaskSetAdapterListView.java:90)
12-20 16:04:15.347: E/AndroidRuntime(13344): at
RanhTV.Utils.AsyncTaskSetAdapterListView.onPostExecute(AsyncTaskSetAdapterListView.java:1)
12-20 16:04:15.347: E/AndroidRuntime(13344): at
android.os.AsyncTask.finish(AsyncTask.java:632) 12-20 16:04:15.347:
E/AndroidRuntime(13344): at
android.os.AsyncTask.access$600(AsyncTask.java:177) 12-20
16:04:15.347: E/AndroidRuntime(13344): at
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
12-20 16:04:15.347: E/AndroidRuntime(13344): at
android.os.Handler.dispatchMessage(Handler.java:102) 12-20
16:04:15.347: E/AndroidRuntime(13344): at
android.os.Looper.loop(Looper.java:136) 12-20 16:04:15.347:
E/AndroidRuntime(13344): at
android.app.ActivityThread.main(ActivityThread.java:5017) 12-20
16:04:15.347: E/AndroidRuntime(13344): at
java.lang.reflect.Method.invokeNative(Native Method) 12-20
16:04:15.347: E/AndroidRuntime(13344): at
java.lang.reflect.Method.invoke(Method.java:515) 12-20 16:04:15.347:
E/AndroidRuntime(13344): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-20 16:04:15.347: E/AndroidRuntime(13344): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 12-20
16:04:15.347: E/AndroidRuntime(13344): at
dalvik.system.NativeStart.main(Native Method)
这个错误是什么意思?我以前对其他 Listview 使用过 AsyncTask 并且没有错误。可能是因为我换了布局?
文件detail_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_
android:layout_
android:weightSum="1"
>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="0.5"
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:weightSum="2"
android:orientation="vertical" >
<LinearLayout android:layout_
android:layout_
android:layout_weight="1"
android:background="#192832">
<com.brightcove.player.view.BrightcoveVideoView
android:id="@+id/bc_player"
android:layout_
android:layout_
android:layout_margin="5dp"
android:background="#000000" >
</com.brightcove.player.view.BrightcoveVideoView>
</LinearLayout>
<LinearLayout android:layout_
android:layout_
android:layout_weight="1"
android:background="#193222">
<TabHost
android:id="@android:id/tabhost"
android:layout_
android:layout_ >
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_
android:layout_
android:layout_weight="0" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_
android:layout_
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_
android:layout_
android:layout_gravity="bottom" />
</LinearLayout>
</TabHost>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="0.5"
android:orientation="vertical">
<ListView
android:layout_
android:layout_
android:id="@+id/List_Episode"
android:layout_marginTop="2dp"
android:listSelector="@drawable/list_selector">
</ListView>
</LinearLayout>
</LinearLayout>
【问题讨论】:
您在旋转手机时是否收到此错误或通常会出现此错误? 我默认使用横向屏幕,而不是旋转手机。谢谢 当您在 Activity 中调用此类时,在该 Activity 中还会创建另一个进度对话框吗? @user2830969 参考链接***.com/questions/2224676/…。它可能会帮助你。 【参考方案1】:尝试在您的 onPostExecute
方法开始时关闭您的进度对话框。
@Override
protected void onPostExecute(List<Map<String, Object>> result)
super.onPostExecute(result);
if (Loading.isShowing())
Loading.dismiss();
..........//your code
.................
【讨论】:
@GrlsHu。你好。拜托我需要你的帮忙。 Hu amdavad no j 6u。请 @GrIsHu 我正在使用换脸应用程序。所以基本上我正在使用手势。但我无法正确修复。功能工作正常。但是当我将卡通类型的图像设置为主图片并触摸该图像视图时,它会出现在屏幕外。我想阻止它。如何?? 你不能用你的问题发布你的问题吗? @PiyushGupta 提供您的问题的链接 @GrIsHu 我已经删除了。因为没有帮助所有人都被否决了。我的所有功能都在工作。也使用多点触控,但我无法获得正确的结果【参考方案2】:试试这个,
public AsyncTaskSetAdapterListViewDetail(final String query, final Context ctx, View listv, HashMap<String, Object> Data)
this.query = query;
this.ctx = ctx;
if(this.Loading!=null)
if (Loading.isShowing())
Loading.dismiss();
this.Loading = new ProgressDialog(ctx);
this.listv = listv;
this.Data = Data;
【讨论】:
谢谢,但还是不行。错误被删除但 ProgressDialog 未完成(关闭) 在你的 postExecute 方法中关闭你的对话框,如果(Loading!=null) Loading.dismiss();
@user2830969 上面的修改你试过了吗?
现在你还不能关闭你的加载器吗?你能重新发布你现在拥有的代码吗?以上是关于由 AsyncTask 调用时 ProgressDialog.dimiss 方法出错的主要内容,如果未能解决你的问题,请参考以下文章
在从服务调用的 asyncTask 中显示 alertDialog 时出错?
如何在将 Tablayout 与 viewpager 一起使用时从另一个片段调用 AsyncTask?
从 Activity 调用时,另一个 AsyncTask 类中的 ProgressDialog 未显示