Autocompletetextview into popup crash my APP

I'm trying to add an autocomplete textview on a Popup window (I copy my code below) but when I start to type the second letter it always crash the application and the code that android studio shows is:

FATAL EXCEPTION: main Process: com.guille.inventario, PID: 3475 android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@62699a9 is not valid; is your activity running?

May you help me please?

fun popup(){
    // Initialize a new layout inflater instance
    val inflater:LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    // Inflate a custom view using layout inflater
    val view = inflater.inflate(R.layout.activity_buscar,null)

    // Initialize a new instance of popup window
    val popupWindow = PopupWindow(
        view, // Custom view to show in popup window
        LinearLayout.LayoutParams.WRAP_CONTENT, // Width of popup window
        LinearLayout.LayoutParams.WRAP_CONTENT, // Window height
    true
    )

    // Set an elevation for the popup window
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        popupWindow.elevation = 10.0F
    }

    // Create a new slide animation for popup window enter transition
    val slideIn = Slide()
    slideIn.slideEdge = Gravity.TOP
    popupWindow.enterTransition = slideIn

    // Slide animation for popup window exit transition
    val slideOut = Slide()
    slideOut.slideEdge = Gravity.RIGHT
    popupWindow.exitTransition = slideOut

    // Get the widgets reference from custom view
    val tv = view.findViewById<EditText>(R.id.artticulopopEditText)
    val buttonPopup = view.findViewById<ImageButton>(R.id.searchpopImageButton)
    val textView = view.findViewById<AutoCompleteTextView>(R.id.autoCompleteTextView)

    val COUNTRIES = arrayOf(
        "Belgium", "France", "Italy", "Germany", "Spain"
    )

    val adapter: ArrayAdapter<String> = ArrayAdapter(view.context,
        android.R.layout.simple_dropdown_item_1line, COUNTRIES)
    textView.setAdapter(adapter)


    // Set a click listener for popup's button widget
    buttonPopup.setOnClickListener{
        // Dismiss the popup window
        popupWindow.dismiss()

    }


    // Finally, show the popup window on app
    //TransitionManager.beginDelayedTransition(root_layout)
    popupWindow.showAtLocation(
        root_layout, // Location to display popup window
        Gravity.CENTER, // Exact position of layout to display popup
        0, // X offset
        0 // Y offset
    )
}


Read more here: https://stackoverflow.com/questions/65917941/autocompletetextview-into-popup-crash-my-app

Content Attribution

This content was originally published by guille at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: