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

2

11

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?

Seagrave answered 9/10, 2019 at 16:1 Comment(0)
P
12
Toast.makeText(this@YOUR_ACTIVITY_NAME, "Clicked on  " + users.get(position).name, Toast.LENGTH_LONG).show()
Pod answered 9/10, 2019 at 16:7 Comment(3)
That worked, thanks! Could you explain me why? I have seen Toast working with only 'this'.Seagrave
when you are setting addOnItemClickListener in input you are passing an instance of an abstract class so when you call "this" you are referring to inner class instance ( object : OnItemClickListener ) then you have to say that "this" refer to activity class not OnItemClickListener.Pod
Got it. Thank you a lot.Seagrave
T
6
//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...
Tendon answered 29/6, 2020 at 19:20 Comment(3)
The requireActivity() helpedScurrilous
requireActivity() gives error as Unresolved reference: requireActivity. What should I do? Sorry I'm new to Android Development.Tolkan
@ShrirangMahajan Then it might not be a fragment.Tendon

© 2022 - 2024 — McMap. All rights reserved.