0

I am developing an app which takes a picture and does some long processing. During processing I would like to show the status via a ProgressDialog, hence I extended the AsyncTask class as following

private class ResultTask extends AsyncTask<multiCrop, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setTitle("Analyzing, please wait ...");
        progressDialog.setMessage("Loading..");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(multiCrop... mcImgV) {
        String result = null;
        //Long processing that uses view multiCrop and occasional publishProgress() calls, returns String result
    }

    @Override
    protected void onProgressUpdate(String... str) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(str);
        progressDialog.setMessage(str[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        ((TextView)findViewById(R.id.resultText)).append(result);
        progressDialog.dismiss();
    }

}

An instance of ResultTask is created in the onClick handler of one of the buttons in the view as shown below

public void onResultClick(View v){
    ResultTask task = new ResultTask();
    task.execute(babyImageView);
}

When the device is in Landscape the code works without any problem, but if the onResultClick event is triggered in the portrait mode I get an error stating that MainActivity has a LeakedWindow. What am I missing here ?

* Please note that this error occurs even if there is no orientation change

LogCat when error occurs

12-09 21:34:57.080: E/WindowManager(15365):     Activity com.example.jaundicedetect.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@410f5070 that was originally added here
12-09 21:34:57.080: E/WindowManager(15365):     android.view.WindowLeaked: Activity com.example.jaundicedetect.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@410f5070 that was originally added here
12-09 21:34:57.080: E/WindowManager(15365):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:350)
12-09 21:34:57.080: E/WindowManager(15365):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
12-09 21:34:57.080: E/WindowManager(15365):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
12-09 21:34:57.080: E/WindowManager(15365):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
12-09 21:34:57.080: E/WindowManager(15365):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
12-09 21:34:57.080: E/WindowManager(15365):     at android.app.Dialog.show(Dialog.java:286)
12-09 21:34:57.080: E/WindowManager(15365):     at com.example.jaundicedetect.MainActivity$ResultTask.onPreExecute(MainActivity.java:328)
12-09 21:34:57.080: E/WindowManager(15365):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-09 21:34:57.080: E/WindowManager(15365):     at android.os.AsyncTask.execute(AsyncTask.java:511)
12-09 21:34:57.080: E/WindowManager(15365):     at com.example.jaundicedetect.MainActivity.onResultClick(MainActivity.java:524)
12-09 21:34:57.080: E/WindowManager(15365):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 21:34:57.080: E/WindowManager(15365):     at java.lang.reflect.Method.invoke(Method.java:511)
12-09 21:34:57.080: E/WindowManager(15365):     at android.view.View$1.onClick(View.java:3039)
12-09 21:34:57.080: E/WindowManager(15365):     at android.view.View.performClick(View.java:3511)
12-09 21:34:57.080: E/WindowManager(15365):     at android.view.View$PerformClick.run(View.java:14110)
12-09 21:34:57.080: E/WindowManager(15365):     at android.os.Handler.handleCallback(Handler.java:605)
12-09 21:34:57.080: E/WindowManager(15365):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 21:34:57.080: E/WindowManager(15365):     at android.os.Looper.loop(Looper.java:137)
12-09 21:34:57.080: E/WindowManager(15365):     at android.app.ActivityThread.main(ActivityThread.java:4447)
12-09 21:34:57.080: E/WindowManager(15365):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 21:34:57.080: E/WindowManager(15365):     at java.lang.reflect.Method.invoke(Method.java:511)
12-09 21:34:57.080: E/WindowManager(15365):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-09 21:34:57.080: E/WindowManager(15365):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-09 21:34:57.080: E/WindowManager(15365):     at dalvik.system.NativeStart.main(Native Method)
jMathew
  • 1,057
  • 8
  • 13

3 Answers3

0

Try this --

    protected void onPostExecute(String result) {
            super.onPostExecute(result);
   progressDialog.dismiss();

   ((TextView)findViewById(R.id.resultText)).append(result);
         }
keshav
  • 3,235
  • 1
  • 16
  • 22
0

First thing try to use progress dialog inside the UI thread.

        public class LoginScreen extends FragmentActivity {
        ProgressDialog progressDialog;


        @Override
        protected void onCreate(Bundle arg0) {
            // TODO Auto-generated method stub
            super.onCreate(arg0);
            requestWindowFeature(Window.FEATURE_NO_TITLE);

                setContentView(R.layout.activity_login);
    progressDialog = ProgressDialog.show(
                                                LoginScreen.this, null,
                                                "Logging you in...");
                                        progressDialog.setCancelable(false);
    }
public void progressDialogDismiss() {
        LoginScreen.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progressDialog.isShowing())
                    progressDialog.dismiss();
            }
        });
    }
    }

Try to use like this. use it an util class.

Ashwin S Ashok
  • 3,623
  • 2
  • 29
  • 36
0

Sorry people, apparently the error was in the few lines just before ResultTask instance, I was extracting bitmap from the multiCrop view which was outside the bounds of the view.

Thanks for all your suggestions.

jMathew
  • 1,057
  • 8
  • 13