android.view.WindowManager $ BadTokenException:无法添加窗口

时间:2017-04-07 04:17:40

标签: android popupwindow

当用户点击浮动操作按钮时,我在片段中添加弹出窗口,在弹出窗口中我单击浮动操作按钮时添加了自动完成编辑字段,弹出窗口显示成功但是当我单击编辑时它给出了一个错误。 错误是

这是完整的错误。

Process: com.example.mubtadanaqvi.stunexus, PID: 7252
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@4234e238 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:769)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1067)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:966)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:635)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1119)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:973)                                                                                       at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:955)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:548)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

这是我的代码

floatingActionButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                    final View customView = inflater.inflate(R.layout.add_new_skill, null);
                    mPopupWindow = new PopupWindow(
                            customView,
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            true
                    );
                    ImageButton cancel = (ImageButton) customView.findViewById(R.id.close_button);
                    cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mPopupWindow.dismiss();
                        }
                    });

                    skillListAdapter = new ArrayAdapter<>(getContext(),android.R.layout.simple_dropdown_item_1line, skillsList);
                    skillInputField = (AutoCompleteTextView) customView.findViewById(R.id.skill_input);
                    skillInputField.setThreshold(1);
                    skillInputField.setAdapter(skillListAdapter);
                    skillListAdapter.addAll(skillsList);
                    skillListAdapter.notifyDataSetChanged();
                    Button saveButton = (Button) customView.findViewById(R.id.save_skill);
                    saveButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String skillText = skillInputField.getText().toString();
                            if (skillText.isEmpty())
                                makeToast("EMPTY");
                            else {
                                if (ConnectivityListener.isNetwork(getContext())) {
//                                    progressDialog.show();
                                    mPopupWindow.dismiss();
                                    makeToast(skillText);
                                    list.add(new Skill_Item(skillText, 0));
                                    adapter.notifyDataSetChanged();
                                } else
                                    makeToast("connection error!");
                            }
                        }
                    });

                    mPopupWindow.showAtLocation(rootLayout, Gravity.CENTER, 0, 0);



                }
            });

1 个答案:

答案 0 :(得分:0)

例外原因: 活动已完成,但您尝试显示包含已完成活动的上下文的对话框。由于没有窗口让对话框显示android运行时抛出此异常。

异常解决方案: 请使用Android调用的isFinishing()方法来检查此活动是否正在完成:

private final WeakReference<MainActivity> mainActivityWeakRef;
mainActivityWeakRef =new WeakReference<MainActivity>(this);
if (mainActivityWeakRef.get() != null && !mainActivityWeakRef.get().isFinishing()) {
// Your Code
}