0

Can anyone please help me to solve this problem. Here I downloaded data from service, for that I used background service and shown progress dialog. It working in my mobile perfectly ,but same code makes problem in my tablet:

Here is 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

        }
    }

Here is my logcat error:

    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)

Can anyone help on this??

Thanks in advance.

Lokesh
  • 5,180
  • 4
  • 27
  • 42

3 Answers3

2

You can just try this. Sometimes the problem may because the progress dilog may be not intialized correctly. Dont do anythind related to UI in background even if doing something try to make it in runonUIthread outherwise windowleak may happen

 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

            }
    }
Sreedev
  • 6,563
  • 5
  • 43
  • 66
  • thanks for your support Sreedev. I edited my code by your suggestion. Now please look at my logcat error. updated above. – Lokesh Jul 12 '13 at 11:04
  • Can you please check the context you are passing. And also please comfirm it that no UI process is doing in doInBackground(String... arg0). This illegalstateexception shows that the view is not attached properly. Somtimes the context may be missing and progress dilog may not be intialized correctly or Some UI process is taking place inside background. Between also try commenting the progress dilog code. If your background process is doing correctly with any exception then you can confirm it that it the problem of progress dilog. – Sreedev Jul 12 '13 at 11:11
  • Data loaded correctly(commented progress dialog code). and with progress dialog code working fine in mobile but not in tablet – Lokesh Jul 12 '13 at 11:20
  • Which tablet you are using – Sreedev Jul 12 '13 at 11:37
  • Try hide()/dismiss() progress dialog in onPause(), and show() the same on onResume(). – A.Ruppin Jul 12 '13 at 11:41
  • @Sreedev: Tablet Model:GT- P 1000 version: 2.3.3 Samsung – Lokesh Jul 12 '13 at 12:05
0

May be you are passing a wrong context to the progress dialog

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

Are you sure that HomeActivity is not stooped when the progress dialod should be shown?

Nermeen
  • 15,883
  • 5
  • 59
  • 72
0

Hi the issue is with respect to ProgressDialog() which is not hidden during activity switch (same activity with progress bar reopened again).

Try hiding the progress dialog in onPause() and show() the same on onResume().

A.Ruppin
  • 161
  • 6
  • 1
    Check this post also for reference: http://stackoverflow.com/questions/2224676/android-view-not-attached-to-window-manager – A.Ruppin Jul 12 '13 at 11:59