Android with Kotlin error when use RecyclerView in Fragment
14,425
Solution 1
You have forgot to do:
video_recyclerview = rootView.findViewById(R.id.id_of_video_recyclerview) as RecyclerView
So the code be like:-
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_video, container, false)
video_recyclerview = rootView.findViewById(R.id.id_of_video_recyclerview) as RecyclerView // Add this
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
return rootView
}
Solution 2
I have to solve this problem
just change video_recyclerview
to rootView.video_recyclerview
because in fragment you need to know first where is the view you use.
Solution 3
you forgot to initialize the the recyclerview.
video_recyclerview = rootView.findViewById(R.id.video_recyclerview) as RecyclerView
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
Solution 4
Update Your code with below.
RecyclerView Adapter:
class MainAdapter : RecyclerView.Adapter<CustomViewHolder>(){
val videoTitles = listOf("First title", "Second", "3rd", "Moore Title")
//number of item
override fun getItemCount(): Int {
return videoTitles.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
//create view
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val videoTitle = videoTitles.get(position)
holder?.view?.video_title.text= videoTitle
}
}
class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){
val video_title= view.video_title
}
Fragment Activity:
class VideoFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_video, container, false)
video_recyclerview = findViewById(R.id.video_recyclerview) as RecyclerView
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
return rootView
}
}
Author by
Yudha Patria
Updated on June 21, 2022Comments
-
Yudha Patria almost 2 years
I have error said this:
Process: com.example.yudha.kotlinauth, PID: 16435 java.lang.IllegalStateException: video_recyclerview must not be null at com.example.yudha.kotlinauth.fragments.VideoFragment.onCreateView(VideoFragment.kt:30)
RecyclerView Adapter:
class MainAdapter : RecyclerView.Adapter<CustomViewHolder>(){ val videoTitles = listOf("First title", "Second", "3rd", "Moore Title") //number of item override fun getItemCount(): Int { return videoTitles.size } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder { //create view val layoutInflater = LayoutInflater.from(parent.context) val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false) return CustomViewHolder(cellForRow) } override fun onBindViewHolder(holder: CustomViewHolder, position: Int) { val videoTitle = videoTitles.get(position) holder?.view?.video_title.text= videoTitle } } class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){ }
Fragment Activity:
class VideoFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val rootView = inflater.inflate(R.layout.fragment_video, container, false) video_recyclerview.layoutManager = LinearLayoutManager(activity) video_recyclerview.adapter = MainAdapter() return rootView } }
myFragment XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".fragments.VideoFragment"> <android.support.v7.widget.RecyclerView android:id="@+id/video_recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
Recycler view(videorow.xml):
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="wrap_content"> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@mipmap/ic_launcher" /> <TextView android:id="@+id/video_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:text="TextView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView2" /> </android.support.constraint.ConstraintLayout>
-
RobCo almost 6 yearsOr you could move to initialisation to
onViewCreated
-
Jeremy Jao over 5 years@RobCo I don't think that's a good idea bc this will bog down the GUI.
-
Tejas Pandya almost 5 yearswhat if we're using
import kotlinx.android.synthetic.main.blah.*
in that case we dont need initialisation. -
Gastón Saillén almost 5 yearsFor some reason, I used the synthetic too and gave me an error with the LayoutManager, finding it manually did work for me