为什么查找RecyclerView 时findViewById 返回null?

Why does findViewById returning null when searching for the RecyclerView?

为什么 val rv: RecyclerView = findViewById(R.id.material_list_rv) 返回 null?

MainActivity

class MainActivity : AppCompatActivity() {

    var adaptor: MaterialListAdaptor = MaterialListAdaptor()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (savedInstanceState == null) {
            supportFragmentManager.commit {
                setReorderingAllowed(true)
                add<MaterialListFragment>(R.id.fragment_container_view)
            }
        }

        initRecyclerView()
        setRecyclerViewData()

    }

    private fun initRecyclerView(){
        val rv: RecyclerView = findViewById(R.id.material_list_rv)
        rv.layoutManager = LinearLayoutManager(this@MainActivity)
        rv.adapter = adaptor
    }

    private fun setRecyclerViewData(){
        val l = DataSource.createMaterialList()
        adaptor.setDataSet(l)
    }
}

activity_main

<?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=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MaterialListFragment

class MaterialListFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_material_list, container, false)
    }
}

fragment_material_list

<?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=".MaterialListFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/material_list_rv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/material_list_rv_list_item" />

</androidx.constraintlayout.widget.ConstraintLayout>

您正在 MainActivity 中寻找 RecyclerView,但它不存在。 相反,您应该尝试在 FragmentActivity 中访问它。

问题

您在 MainActivity.onCreate 中拨打了 findViewById 的电话。现在还为时过早,此时尚未创建 Fragment 的视图。

您应该在 MaterialListFragment 中寻找风景。如果您在 onCreateView 中调用 findViewById,它将起作用。像这样:

val root = inflater.inflate(R.layout.fragment_material_list, container, false)
val rv = root.findViewById(R.id. material_list_rv)

// Do whatever (eg assign rv to a field in the Fragment to use it later)

return root

一些建议

即使您的代码有效,也请尝试让活动、片段和自定义视图管理它们自己的子视图。

如果上层容器深入到它们的子容器中,非常容易出错(就像这里的那个!)并且您无法更新片段的内部细节和不破坏包含 Activity.

的视图

Activity/Fragment生命周期图

为了给自己定位,请牢记这一点(尽管此图是极度的简化图):