In my application, I have a custom AlertDialog (handled by the system using showDialog ()) that contains a tab with two tabs. There is a spinner on one of the tabs. I can rotate my screen without problems until the counter is open (the counter dialog box is displayed). If the rotator is open during rotation, I get the following:
FATAL EXCEPTION: main java.lang.IllegalArgumentException: View not attached to window manager at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355) at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200) at android.view.Window$LocalWindowManager.removeView(Window.java:432) at android.app.Dialog.dismissDialog(Dialog.java:278) at android.app.Dialog.access$000(Dialog.java:71) at android.app.Dialog$1.run(Dialog.java:111) at android.app.Dialog.dismiss(Dialog.java:268) at android.widget.Spinner.onDetachedFromWindow(Spinner.java:89) at android.view.View.dispatchDetachedFromWindow(View.java:6173) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1164) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1746) at android.view.ViewRoot.doDie(ViewRoot.java:2757) at android.view.ViewRoot.handleMessage(ViewRoot.java:1995) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3683) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)
So...
1 - Is it possible to programmatically close the counter during onPause() ?
2 - Is there any other method I should use?
3 - If there is no right solution, how can I catch this particular exception? (The bad practice that I know, but the failure must be stopped, and since the activity is restored properly after the destruction, it does not matter in any way.
... And please, for the sake of love for everything holy, do not suggest adding android:configChanges="orientation" in your declaration of work. It amazes me how often this is accepted as an official correction for orientation issues.
Change Additional Information
Here is my dialog creation code for the link:
static final int ID_DIALOG_CHOOSER = 1; int pref_location; private Dialog dialog; ... protected Dialog onCreateDialog(int id) { switch(id) { case ID_DIALOG_CHOOSER: dialog = show(ID_DIALOG_CHOOSER); break; } return dialog; } ... showDialog(DialogView.ID_DIALOG_CHOOSER); ... Dialog show(final int dialogType) { if (dialogType == ID_DIALOG_CHOOSER) { AlertDialog.Builder builder = new AlertDialog.Builder(this); // inflate tabhost layout View tabHostLayout = (View) inflater.inflate(R.layout.tabhost_layout, null); FrameLayout tabContent = (FrameLayout) tabHostLayout.findViewById(android.R.id.tabcontent); // inflate tab content layouts and add to tabhost layout LinearLayout tab1 = (LinearLayout) inflater.inflate(R.layout.dialog_tab1, null); tabContent.addView(tab1); LinearLayout tab2 = (LinearLayout) inflater.inflate(R.layout.dialog_tab2, null); tabContent.addView(tab2); // tab setup TabHost tabHost = (TabHost) tabHostLayout.findViewById(R.id.TabHost_Dialog_Tabs); tabHost.setup(); TabHost.TabSpec tab_1 = tabHost.newTabSpec("Category 1"); tab_1.setContent(R.id.LinearLayout_Dialog_Tab1_Content); tab_1.setIndicator(this.getResources().getString(R.string.dialog_tab1), this.getResources().getDrawable(R.drawable.tabhost_icon_selector)); tabHost.addTab(tab_1); TabHost.TabSpec tab_2 = tabHost.newTabSpec("Category 2"); tab_2.setContent(R.id.LinearLayout_Dialog_Tab2_Content); tab_2.setIndicator(this.getResources().getString(R.string.dialog_tab2), this.getResources().getDrawable(R.drawable.tabhost_icon_selector)); tabHost.addTab(tab_2); // spinner setup final Spinner spinner_location = (Spinner) tab1.findViewById(R.id.Spinner_Dialog_Location); String[] locationArrayVals = null; ArrayAdapter<CharSequence> adapter_location = null; locationArrayVals = this.getResources().getStringArray(R.array.location_array_vals); adapter_location = ArrayAdapter.createFromResource(this, R.array.location_array_listdisplay, android.R.layout.simple_spinner_item); adapter_location.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_location.setAdapter(adapter_location); // ok button Button button_ok = (Button) tab1.findViewById(R.id.Button_Dialog_OK); button_ok.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { pref_location = spinner_location.getSelectedItemPosition(); dialog.dismiss(); } }); // create dialog builder.setTitle("Location") .setView(tabHost) .setCancelable(true); dialog = builder.create(); } return dialog; }
Edit using a temporary workaround
For everyone that is interested, I managed to at least stop the crash by subclassing spinner and overriding onDetachedFromWindow() as follows:
public static class CatchingSpinner extends Spinner { public CatchingSpinner(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDetachedFromWindow() { try { super.onDetachedFromWindow(); } catch (IllegalArgumentException e) {
As I said, a workaround. Still working on a solution.: /