Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in the ProgressDialog.dimiss method when called by AsyncTask

I use AsyncTask to get data from a server to build adapter for my listview. But if I use ProgressDialog inside my AsyncTask, I get an error.

Code:

File 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();
        }
    }
}

Error:

12-20 16:04:15.347: E/AndroidRuntime(13344):
java.lang.IllegalArgumentException:
View=com.android.internal.policy.impl.PhoneWindow$DecorView{422262a8
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)

What does this error mean? I have used AsyncTask before for other Listviews and didn't have an error. Perhaps the reason is because I replaced the layout?

File 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_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="2"
            android:orientation="vertical" >

            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#192832">
                <com.brightcove.player.view.BrightcoveVideoView
                    android:id="@+id/bc_player"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:background="#000000" >
                </com.brightcove.player.view.BrightcoveVideoView>
            </LinearLayout>

            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#193222">
                <TabHost
                    android:id="@android:id/tabhost"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >
                        <TabWidget
                            android:id="@android:id/tabs"
                                android:orientation="horizontal"
                            android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="0" />
                        <FrameLayout
                            android:id="@android:id/tabcontent"
                                    android:layout_width="0dp"
                                    android:layout_height="0dp"
                                    android:layout_weight="0" />
                        <android.support.v4.view.ViewPager
                                        android:id="@+id/viewpager"
                                        android:layout_width="fill_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_gravity="bottom" />
                    </LinearLayout>
                </TabHost>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:orientation="vertical">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/List_Episode"
            android:layout_marginTop="2dp"
            android:listSelector="@drawable/list_selector">
        </ListView>
    </LinearLayout>
</LinearLayout>
like image 422
Toai Luong Avatar asked Nov 10 '22 16:11

Toai Luong


1 Answers

Try to dismiss your progressdialog at the start of your onPostExecute method.

 @Override
  protected void onPostExecute(List<Map<String, Object>> result) {
             super.onPostExecute(result);
     if (Loading.isShowing()){
                Loading.dismiss();
            }
           ..........//your code
        .................}
like image 142
GrIsHu Avatar answered Nov 14 '22 21:11

GrIsHu