0

I'm trying to get a popup to show up when I press a button. I wanted to make sure it even shows up before working on the details but the app just crashes when i press it. The commented block is what I originally intended the button to do (just adding an item to the recyclerview) but I decided I wanted to do something a bit different. It only had the problem of crashing when I started adding the popup window stuff. The app still runs fine even if I remove the popup window stuff and leave the commented block commented out. So I don't know if the listadapter class is relevant but ill just include it.

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var listAdapter: ListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        listAdapter = ListAdapter(mutableListOf())

        recyclerView.adapter = listAdapter
        recyclerView.layoutManager = LinearLayoutManager(this)

        btn_add.setOnClickListener {
            /*
            //get text from textbox
            val itemText = et_reminder.text.toString()

            if (itemText.isNotEmpty()){

                //create item with text
                val item = Item (itemText)
                //add to list
                listAdapter.addItem((item))
                //clear textbox
                et_reminder.text.clear()

            }*/
        
            val view : View = LayoutInflater.from(applicationContext).inflate (R.layout.popup_s,null)

            val popup: PopupWindow = PopupWindow(view, 250, 250,true)

            popup.showAtLocation(view, Gravity.NO_GRAVITY, 0,0)
        }
    }

}

ListAdapter.kt

class ListAdapter(

    private val items: MutableList<Item>

): RecyclerView.Adapter<ListAdapter.ListViewHolder>()
{

    class ListViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {

        val itemView: View =
            LayoutInflater.from(parent.context).inflate (R.layout.list_item, parent, false)

        return ListViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ListViewHolder, position: Int) {

        val curr_item = items [position]

        holder.itemView.apply {
 
            tv_reminder.text = curr_item.text;
            cb_check.isChecked = curr_item.checked

            cb_check.setOnCheckedChangeListener { _, isChecked ->
                //changing status of checked
                curr_item.checked = isChecked

                for (i in items.indices.reversed()) {
                    if (items[i].checked) {
                        items.removeAt(i)
                        notifyItemRemoved(i)
                    }
                }
            }

        }
    }

    override fun getItemCount(): Int {
        return items.size
    }

    fun addItem (new_item: Item){
        items.add (new_item)

        notifyItemInserted(items.size - 1)
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/basically_black"

        app:layout_constraintBottom_toTopOf="@+id/et_reminder"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/et_reminder"
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:hint="Reminder"
        android:textSize="20sp"
        android:textColorHint="@color/white_purple"
        android:textColor="@color/white_purple"
        android:background="@color/basically_black"

        android:paddingStart="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_add"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/recyclerView"/>

    <Button
        android:id="@+id/btn_add"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="20sp"
        android:background="@color/basically_black"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:paddingStart="10dp">

    <TextView
        android:id="@+id/tv_reminder"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingTop="0dp"
        android:text="temp"
        android:textColor="@color/white_purple"
        android:textSize="20sp"
        android:maxLines="1"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/cb_check"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/cb_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

popup_s.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <EditText
        android:id="@+id/et_r"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Reminder"
        android:textColor="@color/white_purple"
        android:textColorHint="@color/gray"
        android:textSize="20sp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

    </EditText>

</androidx.constraintlayout.widget.ConstraintLayout>
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
skushu
  • 311
  • 1
  • 3
  • 11

1 Answers1

1

I think that happen because you are trying to pass applicationContext it will give you Unable to add window -- token null is not valid; is your activity running?

try to pass the current activity context instead cause PopupWindow can only be attached to an Activity

 val view : View = LayoutInflater.from(this).inflate (R.layout.popup_s,null)

            val popup = PopupWindow(view, 250, 250,true)

            popup.showAtLocation(view, Gravity.NO_GRAVITY, 0,0)
Ahmed Karam
  • 386
  • 1
  • 7