【问题标题】:Android Spinner Exception when clicked BadTokenException单击 BadTokenException 时出现 Android Spinner 异常
【发布时间】:2018-04-06 08:08:13
【问题描述】:

我在单击微调器时遇到问题(在弹出窗口中)

这是我的弹出窗口:

public class PopupDialog extends PopupWindow  {

    public PopupDialog() {
        super();
        init();
    }

    public PopupDialog(View contentView, int width, int height) {
        super(contentView, width, height);
        init();
    }

    public PopupDialog(View contentView) {
        super(contentView);
        init();
    }

    private void init() {
        this.setTouchable(true);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());

        this.setTouchInterceptor(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    PopupDialog.this.dismiss();

                    return true;
                }

                return false;
            }
        });

    }

}

单击显示弹出窗口的按钮:

public void click(View v) {
        LayoutInflater layoutInflater 
         = (LayoutInflater)getBaseContext()
          .getSystemService(LAYOUT_INFLATER_SERVICE);  
        View popupView = layoutInflater.inflate(R.layout.popup, null);  
                 final PopupDialog popupWindow = new PopupDialog(
                   popupView, 
                   LayoutParams.WRAP_CONTENT,  
                         LayoutParams.WRAP_CONTENT);

                 DisplayMetrics metrics = new DisplayMetrics();
                 getWindowManager().getDefaultDisplay().getMetrics(metrics);
                 popupWindow.setHeight(metrics.heightPixels);
                 popupWindow.setWidth(300);

                 popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, v.getLeft()+v.getWidth(), 0);

    }

我有这个异常 (BadTokenException)

05-29 16:35:10.627: E/AndroidRuntime(1055): FATAL EXCEPTION: main
05-29 16:35:10.627: E/AndroidRuntime(1055): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@410ad298 is not valid; is your activity running?
05-29 16:35:10.627: E/AndroidRuntime(1055):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:515)
05-29 16:35:10.627: E/AndroidRuntime(1055):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
05-29 16:35:10.627: E/AndroidRuntime(1055):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
05-29 16:35:10.627: E/AndroidRuntime(1055):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
05-29 16:35:10.627: E/AndroidRuntime(1055):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
05-29 16:35:10.627: E/AndroidRuntime(1055):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
05-29 16:35:10.627: E/AndroidRuntime(1055):     at and

欢迎任何帮助,提前谢谢你:)

【问题讨论】:

  • 你把LayoutInflater layoutInflater = (LayoutInflater)getBaseContext() .getSystemService(LAYOUT_INFLATER_SERVICE);改成LayoutInflater layoutInflater = (LayoutInflater)v.getContext() .getSystemService(LAYOUT_INFLATER_SERVICE);后试过了吗?
  • 当我更改为 LayoutInflater layoutInflater = (LayoutInflater)v.getContext() .getSystemService(LAYOUT_INFLATER_SERVICE);

标签: android spinner popupwindow


【解决方案1】:

存在上下文冲突。尝试在您的 XML 微调器声明中添加它:android:spinnerMode="dialog"

【讨论】:

  • 是的,谢谢,它在对话模式下工作正常,但我必须在下拉模式下进行,你知道在下拉模式下要改变什么吗?
  • 在我看来这是不可能的,因为 popupWindow 接管了您活动的焦点。
  • 我不知道,但是根据尝试次数和实现它所花费的小时数,当您说这是不可能的时候,我相信您是对的:D 非常感谢。
【解决方案2】:

在这里做一个测试,

暂时删除这部分代码

 this.setTouchInterceptor(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    PopupDialog.this.dismiss();

                    return true;
                }

                return false;
            }
        });

并检查。

【讨论】:

    【解决方案3】:

    我希望现在还为时不晚。 我遇到过同样的问题。一切正常

    android:spinnerMode="dialog"
    

    但是,我无法使用 PopupWindow 实现下拉菜单(没有弹出对话框)。 我通过使用 alertbuilder 得到了相同的工作。

    您可以按照本教程进行操作: YT Link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      相关资源
      最近更新 更多