尝试显示popupWindow时出现IllegalStateException

时间:2014-06-23 10:53:33

标签: android popupwindow illegalstateexception

我有以下代码。这很简单。每当我按下菜单项时,我希望能够生成一个popupWindow。但是,我不明白这个错误

06-23 19:32:51.996: E/AndroidRuntime(8237): FATAL EXCEPTION: main
06-23 19:32:51.996: E/AndroidRuntime(8237): Process: com.package, PID: 8237
06-23 19:32:51.996: E/AndroidRuntime(8237): java.lang.IllegalStateException: View org.holoeverywhere.widget.LinearLayout{436bbf70 V.E..... ......ID 0,0-0,0} has already been added to the window manager.
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:230)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.widget.PopupWindow.invokePopup(PopupWindow.java:462)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.widget.PopupWindow.showAtLocation(PopupWindow.java:683)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.widget.PopupWindow.showAtLocation(PopupWindow.java:687)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.package.DeviceSelectionActivity.showPopupFilter(DeviceSelectionActivity.java:198)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.package.DeviceSelectionActivity.onOptionsItemSelected(DeviceSelectionActivity.java:140)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.app.Activity.onOptionsItemSelected(Activity.java:338)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.app.Activity.onMenuItemSelected(Activity.java:2600)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:366)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.app.Activity.access$6(Activity.java:1)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.app.Activity$13.post(Activity.java:305)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.addon.IAddonBasicAttacher.performAddonAction(IAddonBasicAttacher.java:81)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.app.Activity.performAddonAction(Activity.java:523)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at org.holoeverywhere.app.Activity.onMenuItemSelected(Activity.java:297)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.view.View.performClick(View.java:4438)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.view.View$PerformClick.run(View.java:18422)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.os.Handler.handleCallback(Handler.java:733)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.os.Looper.loop(Looper.java:136)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at java.lang.reflect.Method.invoke(Method.java:515)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-23 19:32:51.996: E/AndroidRuntime(8237):     at dalvik.system.NativeStart.main(Native Method)

下面添加了我的活动

View filterView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    setContentView(R.layout.main_activity);

    filterView = inflater.inflate(R.layout.popup_layout, null, false);

}

我需要filterView是全局的,因为我在我的一些方法中也使用它

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.device_selection_filter:
        showPopupFilter();
        break;

    }
    return super.onOptionsItemSelected(item);
}

private void showPopupFilter(){

    final PopupWindow filters = new PopupWindow(filterView, getWindowManager().getDefaultDisplay().getWidth(), 800, true);
    filters.showAtLocation(findViewById(R.id.parent), Gravity.BOTTOM, 0, 0);

    FluxTextView tv = ((FluxTextView) (filterView.findViewById(R.id.click)));
    tv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            filters.dismiss();
        }
    });

}

1 个答案:

答案 0 :(得分:1)

事实证明,我的代码中实际上没有错误。我设法通过在我的案例之后添加return true;而不是break;来修复它。

switch (item.getItemId()) {

case R.id.device_selection_filter:
    showPopupFilter();
    return true;
}

(如果我错了,任何人都会纠正我)会发生什么事情onOptionsItemSelected()每次点击后都会运行两次。这导致弹出布局被添加TWICE,因此

 IllegatStateException ... has already been added to the window manager.

我希望将来可以帮助某人。