android-fragments 使用Kotlin初始化片段中的RecyclerView时收到NullPointerException

wgmfuz8q  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(140)

我是一个新手,还在学习Kotlin,我正在尝试在一个片段中实现回收器视图。但是,我收到了NullPointerException错误

java.lang.NullPointerException: view.findViewById(R.id.recycler_view) must not be null

我已检查回收程序视图的ID

<?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>

我还通过使用**view.findViewById()在片段的onViewCreated()**中初始化了回收器视图

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()
    }

不过,不行啊,你们能不能帮帮我?

2nbm6dog

2nbm6dog1#

您使用错误的view调用findViewById,有一些方法可以解决您的问题:
1.将回收器视图从activity.xml移动到fragment.xml
1.您可以从片段中调用回收程序,如下所示:

activity?.findViewById.....vv
ljsrvy3e

ljsrvy3e2#

请在代码中尝试以下操作

eventRecyclerView = findViewById<RecyclerView>(R.id.recycler_view)

eventRecyclerView = view.findViewById<RecyclerView>(R.id.recycler_view)

相关问题