FY Gamer
FY Gamer

Reputation: 297

Error "None of the following functions can be called with the arguments supplied:" with Toast

I want to create a code to click on items of RecyclerView. I found one from Internet, however it keep getting this error:

None of the following functions can be called with the arguments supplied:

public open fun makeText(p0: Context!, p1: CharSequence!, p2: Int): Toast! defined in android.widget.Toast

public open fun makeText(p0: Context!, p1: Int, p2: Int): Toast! defined in android.widget.Toast

Here's my code:

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

        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        val users = ArrayList<User>()

        val adapter = CustomAdapter(users)

        recyclerView.adapter = adapter

        recyclerView.addOnItemClickListener(object : OnItemClickListener {
            override fun onItemClicked(position: Int, view: View) {
                Toast.makeText(this, "Clicked on  " + users.get(position).name, Toast.LENGTH_LONG).show()
            }
        })


    }

    interface OnItemClickListener {
        fun onItemClicked(position: Int, view: View)
    }

    fun RecyclerView.addOnItemClickListener(onClickListener: OnItemClickListener) {
        this.addOnChildAttachStateChangeListener(object : RecyclerView.OnChildAttachStateChangeListener {
            override fun onChildViewDetachedFromWindow(view: View) {
                view.setOnClickListener(null)
            }

            override fun onChildViewAttachedToWindow(view: View) {
                view.setOnClickListener {
                    val holder = getChildViewHolder(view)
                    onClickListener.onItemClicked(holder.adapterPosition, view)
                }
            }
        })
    }

How can I fix that error message?

Upvotes: 11

Views: 20729

Answers (2)

Dharmik Thakkar
Dharmik Thakkar

Reputation: 2168

//In Activity use: 
Toast.makeText(this@YOUR_ACTIVITY_NAME, "your message", Toast.LENGTH_LONG).show()
    
//In Fragments use: 
Toast.makeText(requireActivity(), "your message", Toast.LENGTH_LONG).show()
 
Your problem will be solved...

Upvotes: 6

Amir Abbas
Amir Abbas

Reputation: 395

Toast.makeText(this@YOUR_ACTIVITY_NAME, "Clicked on  " + users.get(position).name, Toast.LENGTH_LONG).show()

Upvotes: 12

Related Questions