Android Fragments java.lang.NullPointerException:findViewById< RecyclerVie...(R.id.recyclerView)不能为null

jgzswidk  于 2023-06-06  发布在  Android
关注(0)|答案(2)|浏览(318)

我试图设置回收器视图来处理我创建的片段,在MainActivity中,在“瓦尔recyclerV = findViewById(R.id.plantsRecyclerView)”之后,我得到“java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}:java.lang.NullPointerException:findViewById<RecyclerVie...(R.id.plantsRecyclerView)must not be null”即使具有该ID的回收器存在,我如何解决这个问题?先谢谢你了!
(code:主要活动:

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

MyRecyclerViewAdapter:

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

fragment 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"
    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>

MainFragment(我在创建后没有改变任何东西,我想这就是问题所在):

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

z3yyvxxp1#

因为您的RecyclerView位于MainFragment中。不在主活动中。在MainFragment中创建RecyclerView。

wsxa1bj1

wsxa1bj12#

将layout设置为activity_main -因为我们找不到在activity_main布局setContentView(R.layout.activity_main)中创建的回收器视图

相关问题