3

I created a PopupWindow that displays a list of buttons. Originally, for each item in my list, I implemented an OnLongClickListener to display a Toast message in the view holder:

private inner class MenuItemViewHolder (itemView: View) {

    val button: ImageButton = itemView.findViewById(R.id.button);

    fun bindItem(item: MenuItem) {
        button.setImageDrawable(item.icon)
        button.isEnabled = item.isEnabled

        button.setOnClickListener { popupWindow.onItemClick(item) }
        button.setOnLongClickListener {
            Toast.makeText(context, item.title, Toast.LENGTH_SHORT).show()
        }
    }

}

I wanted to update this to display a tooltip message instead:

TooltipCompat.setTooltipText(button, item.title)

But when I long click the button, I get the following error:

10-06 16:32:35.470 22492-22492/com.example.app.ui E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.app.ui, PID: 22492
        android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@e214c8e is not valid; is your activity running?
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
            at com.android.internal.view.TooltipPopup.show(TooltipPopup.java:74)
            at android.view.View.showTooltip(View.java:26365)
            at android.view.View.showLongClickTooltip(View.java:26389)
            at android.view.View.performLongClickInternal(View.java:6344)
            at android.view.View.performLongClick(View.java:6294)
            at android.view.View.performLongClick(View.java:6312)
            at android.view.View$CheckForLongPress.run(View.java:24662)
            at android.os.Handler.handleCallback(Handler.java:789)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6541)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

I found some answers to similar questions, which all seem to suggest that the Activity the View belongs to is not in the correct state when they try to add a new Window. But the Activity is definitely running before I perform a long press on the button, and no destructive lifecycle methods have been called (i.e. onPause, onStop, onDestroy).

What could be causing this error? Could it have something to do with the fact that the ListView is being displayed in a PopupWindow? If so, how can I remedy the situation?


I added the necessary code to a GitHub project. If you run the application, you will notice that the tooltip works fine on the menu button (overflow button). But when you open the menu and long press on any of the icons, the application crashes with the above error.

Bryan
  • 14,756
  • 10
  • 70
  • 125
  • Can you post a simple project on github with that behavior? – azizbekian Oct 17 '17 at 06:38
  • @azizbekian Updated with a link. – Bryan Oct 17 '17 at 16:33
  • 1
    Have you seen this [bug report](https://issuetracker.google.com/issues/64461213)? This looks like your issue but it is unclear if the changes have been released or not. My guess for your issue is that the tooltip popup is being attached to the wrong window. – Cheticamp Oct 18 '17 at 15:08
  • @Cheticamp I had not! Good to know it's not just me. The issue is marked as fixed, but it still hasn't been fixed as of 26.1.0; I guess I'll have to stick with using a toast until it is. – Bryan Oct 18 '17 at 16:03
  • 1
    If you haven't already, you may want to star the bug report so you will be notified when (and if) they update the report. I took a look at a work-around, but it would involve a deep dive into the windows management system. I think the toast is the way to go for the time being. – Cheticamp Oct 18 '17 at 16:12
  • @Cheticamp Already have. I tried digging into the code a bit myself; not worth the effort. In any case, post this as an answer and I'll give you the bounty! – Bryan Oct 18 '17 at 16:15

1 Answers1

2

Have you seen this bug report? This looks like your issue but it is unclear if the changes have been released or not. My guess for your issue is that the tooltip popup is being attached to the wrong window.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131