Android java.lang.NullPointerException:findViewById

Android java.lang.NullPointerException:findViewById,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,我正在尝试设置recycler视图以处理我创建的片段,在val recyclerV=findViewByIdR.id.plantsRecyclerView之后的MainActivity中,我得到了java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}:java.lang.NullPointerException:f

我正在尝试设置recycler视图以处理我创建的片段,在val recyclerV=findViewByIdR.id.plantsRecyclerView之后的MainActivity中,我得到了java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}:java.lang.NullPointerException:findViewById,因为您的RecyclerView位于MainFragment中。不在活动中。在MainFragment中创建您的RecyclerView。

这样就不能在片段中找到RecyclerView?当然可以。在片段中调用findViewByID以获取RecyclerView。事实证明,我完全误解了片段是什么,但现在我已经修复了所有我刚刚必须阅读的文档…,非常感谢!
private var numPlants = 4
class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //there is some parts that I'm not posting because they are not relevant (at least I think so <it's mainly code that creates notifications>)
        val recyclerV = findViewById<RecyclerView>(R.id.plantsRecyclerView)
        recyclerV.layoutManager = LinearLayoutManager(this)
        recyclerV.adapter = MyRecyclerViewAdapter(this, numPlants)
    }
}
package com.example.myapplication

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class MyRecyclerViewAdapter(private val context: Context, private val numPlants: Int) :
        RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.card_layout, parent, false)
        return ViewHolder(view)
    }

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

    override fun getItemCount() = numPlants

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        fun bind(position: Int){
            // not implemented yet
        }
    }
}
<?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"
    android:background="@color/darker_green"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/plantsRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [MainFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class MainFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    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? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment MainFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            MainFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}