Kotlin showed type mismatch in fragment

12,546

Solution 1

Change this to-:

recyclerView!!.adapter = RestaurantMenuAdapter(activity)

To-:

recyclerView!!.adapter = RestaurantMenuAdapter(activity.applicationContext)

Solution 2

recyclerView!!.adapter = RestaurantMenuAdapter(this)

To

recyclerView!!.adapter = RestaurantMenuAdapter(this.requireActivity())

Solution 3

Keep the adapter as it and just use "activity!!" where you are initializing adapter.

recyclerView.adapter = RestaurantMenuAdapter(activity!!)

Your adapter will remain same.

class RestaurantMenuAdapter  (val context : Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    return object : RecyclerView.ViewHolder(inflater.inflate(R.layout.item_menu1, parent, false)) {

    }
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

}

override fun getItemCount(): Int {
    return 10
}
}
Share:
12,546
Long Ranger
Author by

Long Ranger

Learn code every day and save time for my life. My First Kotlin project https://github.com/charlesng/Vegas Currently I am trying to make a sample application with the new android app architecture. Feel free to make any comment on it. https://github.com/charlesng/SampleAppArch

Updated on June 06, 2022

Comments

  • Long Ranger
    Long Ranger about 2 years

    I have a fragment with RecyclerAdapter inside it. I want to initialize the adapter in the onCreateView method but it throws the error of "Type mismatch. Required : Context , Found : FragmentActivity" in this statement

    I have no idea why the first one showed this error and the second one did not contains compile time error.

    Error shown

    recyclerView!!.adapter = RestaurantMenuAdapter(activity)
    

    No Error shown

    recyclerView!!.layoutManager = LinearLayoutManager(activity)
    

    Fragment.kt

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_restaurant_menu, container, false)
        recyclerView = view.findViewById(R.id.restaurant_container)
        recyclerView!!.adapter = RestaurantMenuAdapter(activity)
        recyclerView!!.layoutManager = LinearLayoutManager(activity)
    

    RecyclerAdapter.kt

    class RestaurantMenuAdapter  (val context : Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            return object : RecyclerView.ViewHolder(inflater.inflate(R.layout.item_menu1, parent, false)) {
    
            }
        }
    
        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    
        }
    
        override fun getItemCount(): Int {
            return 10
        }
    }
    
  • Shivam Oberoi
    Shivam Oberoi over 6 years
    are you extending Fragment or FragmentActivity?
  • Long Ranger
    Long Ranger over 6 years
    Yes, the Fragment extends to android.support.v4.app.Fragment
  • Shivam Oberoi
    Shivam Oberoi over 6 years
    do this then class RestaurantMenuAdapter (val context : Activity)
  • Long Ranger
    Long Ranger over 6 years
    It seems this applicationContext works. But it showed I still need to add this activity!!.applicationContext before I passed to the adapter.
  • Shivam Oberoi
    Shivam Oberoi over 6 years
    Do one thing change this - (val context : Context) to-(val context : FragmentActivity) and do the same code below.