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)