【问题标题】:No Adapter attached in Bottom Navigation底部导航中没有附加适配器
【发布时间】:2018-12-12 19:42:26
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-studio kotlin kotlin-android-extensions


    【解决方案1】:

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

    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
    }
    

    【讨论】:

    • 感谢@Onur D 的回复,但我的应用程序在上述更改后崩溃,以下是错误。 2018-12-12 13:10:52.742 16606-16606/com.kodechamp.bottomnavigationwithfragmentsinkotlin E/AndroidRuntime:致命异常:主进程:com.kodechamp.bottomnavigationwithfragmentsinkotlin,PID:16606 java.lang.IllegalStateException:recyclerView 不能为空com.kodechamp.bottomnavigationwithfragmentsinkotlin.HomeFragment.onCreateView(HomeFragment.kt:46)
    • 我没有注意到您的代码中也没有引用您的回收站视图。请添加此val recyclerView = view.findViewById(R.id.your_recycler_view_id) 我还编辑了我的答案。
    • @你太棒了。它对我有用。我根据 kotlin 语法更改了 recycler 的持有参考值。再次感谢您的帮助。
    • @JoshvaJo 很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多