Simo
Simo

Reputation: 1230

Android Spinner Exception when clicked BadTokenException

I have a problem when clicking on a spinner ( in a popup window )

here is my popup:

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;
            }
        });

    }

}

the click of the button that shows the popup:

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);

    }

I have this exception ( 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

Any help is welcome, thank you in advance :)

Upvotes: 3

Views: 1599

Answers (3)

Joohay
Joohay

Reputation: 51

I hope it is not too late. I had the same issue. Things were working fine with

android:spinnerMode="dialog"

However, I was unable to achieve a dropdown (without dialog popup) with the PopupWindow. I got the same working by using alertbuilder instead.

You can follow this tutorial: YT Link

Upvotes: 0

moud
moud

Reputation: 749

There is a context conflict. Try adding this in your XML spinner declaration : android:spinnerMode="dialog"

Upvotes: 9

Bharat Jyoti
Bharat Jyoti

Reputation: 394

Do a test here,

Remove temporarily this part of your code

 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;
            }
        });

and check.

Upvotes: 0

Related Questions