底部导航中没有连接适配器

No Adapter attached in Bottom Navigation

我正在尝试使用回收站视图在底部导航 activity 中显示项目,但我没有获得项目值。

我的项目中有三种布局 activity_main.xml、list_row.xml 和 fragment_home.xml。 Recycler 视图在 fragment_home.xml 和 list_row.xml 中包含两个文本视图。我在代码中没有看到任何错误,但在日志中,我收到了 No adapter attached 消息。非常感谢您的帮助。

    Error:
    E/RecyclerView: No adapter attached; skipping layout

    Below is the HomeFragment.kt code.

    class HomeFragment : Fragment() {

        private var adapter:PersonListAdapter?=null
        private var personList:ArrayList<Person>?=null
        private var layoutManager: RecyclerView.LayoutManager?=null


        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {

         return inflater.inflate(R.layout.fragment_home, container, false)

            personList=ArrayList<Person>()
            layoutManager= LinearLayoutManager(this.context)
            adapter= PersonListAdapter(personList, this.context!!)

           recyclerView.layoutManager=layoutManager
            recyclerView.adapter=adapter

            for (i in 0..16) {
                val person = Person()
                person.name="Hello" + i
                person.age = 20 + i
                personList!!.add(person)

            }
            adapter!!.notifyDataSetChanged()
        }
    }

    MainActivity.kt code

    class MainActivity : AppCompatActivity() {

        private var adapter:PersonListAdapter?=null
        private var personList:ArrayList<Person>?=null
        private var layoutManager: RecyclerView.LayoutManager?=null

        private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener {item->
            when(item.itemId){
                R.id.home -> {
                    println("home pressed")
                    replaceFragment(HomeFragment())
                    return@OnNavigationItemSelectedListener true
                }
                 }

            false

        }


        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
            replaceFragment(HomeFragment())


        }


        private fun replaceFragment(fragment: Fragment){
            val fragmentTransaction = supportFragmentManager.beginTransaction()
            fragmentTransaction.replace(R.id.fragmentContainer, fragment)
            fragmentTransaction.commit()
        }
    }
Data Class:
class PersonListAdapter(private val list: ArrayList<Person>?, private val context: Context): RecyclerView.Adapter<PersonListAdapter.ViewHolder>()
{
    override fun getItemCount(): Int {

        return list!!.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
        val view= LayoutInflater.from(context).inflate(R.layout.list_row,parent,false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
        p0?.bindItem(list?.get(p1))
    }
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    {
        fun bindItem(person: Person?)
        {
            var name: TextView =itemView.findViewById(R.id.name) as TextView
            var age: TextView =itemView.findViewById(R.id.age) as TextView
            name.text=person!!.name
            age.text = person.age.toString()
        }
    }
}

您在 HomeFragment 的 onCreateView() 中做的第一件事是 return 展开您的布局。所以你在 onCreateView 方法中的所有代码都是不可触及的。相反,只需保留对您的膨胀布局的引用,并在最后 return 它。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {

     val view = inflater.inflate(R.layout.fragment_home, container, false)
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)

        personList=ArrayList<Person>()
        layoutManager= LinearLayoutManager(this.context)
        adapter= PersonListAdapter(personList, this.context!!)

       recyclerView.layoutManager=layoutManager
        recyclerView.adapter=adapter

        for (i in 0..16) {
            val person = Person()
            person.name="Hello" + i
            person.age = 20 + i
            personList!!.add(person)

        }
        adapter!!.notifyDataSetChanged()

        return view
}