I am a newbie who is still learning Kotlin, I am trying to implement recycler view inside a fragment. However, I received the NullPointerException error
java.lang.NullPointerException: view.findViewById(R.id.recycler_view) must not be null
I have checked the id of recycler view
<?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=".EventListActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recycler_view"
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/event_item"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
I have also initialized the recycler view in the onViewCreated() of the fragment by using view.findViewById()
class EventFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private lateinit var databaseReference: DatabaseReference
private lateinit var eventRecyclerView: RecyclerView
private lateinit var eventArrayList: ArrayList<Event>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
(activity as AppCompatActivity).supportActionBar?.title = "Event"
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_event, container, false)
}
companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
EventFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
eventRecyclerView = view.findViewById(R.id.recycler_view)
eventRecyclerView.layoutManager = LinearLayoutManager(context)
eventRecyclerView.setHasFixedSize(true)
eventArrayList = arrayListOf<Event>()
getEventData()
}
However, it didn't work out, can you guys please help me on this?
CodePudding user response:
You call findViewById with wrong view, there are somes way to resolved your problem:
move your recyclerView from activity.xml to fragment.xml
you can call recycler from fragment like this:
activity?.findViewById.....vv
CodePudding user response:
try this in your code
eventRecyclerView = findViewById<RecyclerView>(R.id.recycler_view)
or
eventRecyclerView = view.findViewById<RecyclerView>(R.id.recycler_view)