Android:在alertdialog中的spinner崩溃了

时间:2017-09-23 10:36:54

标签: java android spinner android-spinner alertdialog

我在alertdialog中有旋转器。如果显示alertdialog并且屏幕方向已更改 - 一切正常。 Alertdialog仍然显示。但是如果微调器处于打开状态并且屏幕方向发生了变化,则应用程序崩溃,View未附加到窗口管理器。我在onDestroy中尝试过alertdialog.dismiss(),但没有成功。有什么想法吗?

LayoutInflater li = LayoutInflater.from(ScrollingActivity.this);
View promptsView = li.inflate(R.layout.popup_layout, null);
AlertDialog.Builder alertDialogBuilder = new 
AlertDialog.Builder(ScrollingActivity.this, R.style.MyAlertDialogStyle);

alertDialogBuilder.setView(promptsView);

alertDialogBuilder.setTitle(R.string.def_recipe_lang);
alertDialogBuilder.setIcon(R.drawable.ic_language_orange_24px);
// create alert dialog
alertDialog = alertDialogBuilder.create(); 
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);

错误:     09-23 15:06:27.745 15519-15519 / com.example.user.coordinatortest E / AndroidRuntime:FATAL EXCEPTION:main                                                                                   java.lang.IllegalArgumentException:视图未附加到窗口管理器                                                                                       在android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:385)                                                                                       在android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:287)                                                                                       在android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)                                                                                       在android.app.Dialog.dismissDialog(Dialog.java:323)                                                                                       在android.app.Dialog.dismiss(Dialog.java:306)                                                                                       在android.widget.Spinner $ DialogPopup.dismiss(Spinner.java:840)                                                                                       在android.widget.Spinner.onDetachedFromWindow(Spinner.java:414)                                                                                       在android.support.v7.widget.AppCompatSpinner.onDetachedFromWindow(AppCompatSpinner.java:399)                                                                                       在android.view.View.dispatchDetachedFromWindow(View.java:12024)                                                                                       在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2612)                                                                                       在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2610)                                                                                       在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2610)                                                                                       在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2610)                                                                                       在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2610)                                                                                       在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2610)                                                                                       在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2610)                                                                                       在android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2610)                                                                                       在android.view.ViewRootImpl.dispatchDetachedFromWindow(ViewRootImpl.java:2950)                                                                                       在android.view.ViewRootImpl.doDie(ViewRootImpl.java:4535)                                                                                       在android.view.ViewRootImpl $ ViewRootHandler.handleMessage(ViewRootImpl.java:3318)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:107)                                                                                       在android.os.Looper.loop(Looper.java:194)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5431)                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)                                                                                       在java.lang.reflect.Method.invoke(Method.java:525)                                                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)                                                                                       在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:0)

"我在onDestroy"中尝试过alertdialog.dismiss(),这是正确的,但你必须在Onpause()而不是在Ondestroy()中这样做。

Ondestroy将在销毁屏幕时执行,而不是在方向更改时执行。

尝试此操作,如果您收到错误,请发布您的代码和Android监视器错误以获得适当的答案。