0

My andorid app works fine in most cases but sometimes crashes when the phone rotates, i get the error log below which I don't understand. Can anyone explain why this happens?

Here is the error log :

02-22 14:44:52.175: D/AndroidRuntime(26784): Shutting down VM
02-22 14:44:52.175: W/dalvikvm(26784): threadid=1: thread exiting with uncaught exception (group=0x40e81300)
02-22 14:44:52.183: E/AndroidRuntime(26784): FATAL EXCEPTION: main
02-22 14:44:52.183: E/AndroidRuntime(26784): java.lang.IllegalArgumentException: View not attached to window manager
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:653)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:349)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:160)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.app.Dialog.dismissDialog(Dialog.java:319)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.app.Dialog.dismiss(Dialog.java:302)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at com.example.chartviewer.JsonActivity$getChartItems.onPostExecute(JsonActivity.java:267)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at com.example.chartviewer.JsonActivity$getChartItems.onPostExecute(JsonActivity.java:1)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.os.Looper.loop(Looper.java:137)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at android.app.ActivityThread.main(ActivityThread.java:4745)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at java.lang.reflect.Method.invokeNative(Native Method)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at java.lang.reflect.Method.invoke(Method.java:511)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-22 14:44:52.183: E/AndroidRuntime(26784):    at dalvik.system.NativeStart.main(Native Method)

Update

Here is the code for the the onPostExecuteMethod:

//Removes the progress dialog when the data has been fetched

        protected void onPostExecute(String args) {
            progressDialog.dismiss();

            //Shows alert dialog if data is unavailable
            if(args != null && args.equals(noData)){

                 AlertDialog.Builder builder = new AlertDialog.Builder(JsonActivity.this);
                 builder.setIcon(R.drawable.artistlogo);
                 builder.setTitle("Musicmetric Charts");
                 builder.setMessage(noData);
                 builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        // finishes the activity and leads back to the list of charts 
                        JsonActivity.this.finish();

                    }

                });

                 AlertDialog alert = builder.create();
                 alert.show();

            }



        }
AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56
  • Some code will be useful. Looks like You're trying to dismiss already dissmissed/detached dialod in getChartItems.onPostExecute – sandrstar Feb 22 '13 at 15:00
  • @sandrstar updated with code. – AndroidEnthusiast Feb 22 '13 at 15:11
  • Consider using a `Loader` and `LoaderManager` and/or a `Fragment` with `setRetainInstance(true)` set. Either one will be "reattached" to the Activity at the other side of the configuration change. – Karakuri Feb 22 '13 at 15:41

2 Answers2

4

These are the 2 relevant lines in your stacktrace:

com.example.chartviewer.JsonActivity$getChartItems.onPostExecute(JsonActivity.java:267)

You have implemented the onPostExcecute method of an AsyncTask. This method is dismissing a dialog:

android.app.Dialog.dismiss(Dialog.java:302)

When you rotate the device, the AsyncTask continues to run and tries to hide a dialog that isn't visible anymore.

ashtom
  • 804
  • 1
  • 8
  • 13
0

to expand ashtom's answer: try to maintain device rotation by yourself:

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE)){
        // TODO
    }else if (newConfig.equals(Configuration.ORIENTATION_PORTRAIT)){
        // TODO
    }
}
Droidman
  • 11,485
  • 17
  • 93
  • 141
  • hi, i'm not sure how to manage the configuration myself. In the case that the configuration changes is it best to dismiss the progress dialog then? – AndroidEnthusiast Feb 22 '13 at 15:23
  • When the device is rotated, the Activity gets destroyed and re-created. Please take a look at this question, I think what will solve your problem: http://stackoverflow.com/questions/7128670/best-practice-asynctask-during-orientation-change – Droidman Feb 22 '13 at 15:35
  • Note that this solution only helps for rotation. The AsyncTask might continue to run when the app is in the background (e.g. when a call came in), so you always want to check if the dialog is visible before dismissing it. – ashtom Feb 22 '13 at 16:05
  • @Maver1ck just to clarify if i was only working with one orientation for the activity there would be no need to handle rotation right? – AndroidEnthusiast Feb 22 '13 at 16:16
  • sure. This topic is only relevant if want to support both orientations – Droidman Feb 22 '13 at 16:28