简体   繁体   English

Kotlin Android Fragment recyclerView 和上下文问题

[英]Kotlin Android Fragment recyclerView and context issue

I would like to create an recyclerView in a fragment, but it shows an error " java.lang.IllegalStateException: recylerView_Main must not be null at com.gph.bottomnavigation.FragmentMe.onCreateView(FragmentMe.kt:28)"我想在片段中创建一个 recyclerView,但它显示错误“java.lang.IllegalStateException: recylerView_Main must not be null at com.gph.bottomnavigation.FragmentMe.onCreateView(FragmentMe.kt:28)”

  • Question 1) Please kindly help to solve this issue.问题 1) 请帮助解决这个问题。
  • Question 2) I created an recyclerView only in a empty project without any fragment, it is working properly.问题2)我只在一个没有任何片段的空项目中创建了一个recyclerView,它工作正常。

在此处输入图片说明

But the same code is no working in Fragment, it shows error so I change "recylerView_Main.layoutManager = LinearLayoutManager(this)" to "recylerView_Main.layoutManager = LinearLayoutManager(context)" It shows no error and I can run in simlulator, but when I click the navigation button of the Fragment, the app stops and show this error.但是相同的代码在 Fragment 中不起作用,它显示错误,所以我将“recylerView_Main.layoutManager = LinearLayoutManager(this)”更改为“recylerView_Main.layoutManager = LinearLayoutManager(context)”它没有显示错误,我可以在模拟器中运行,但是当我单击 Fragment 的导航按钮,应用程序停止并显示此错误。 Please kindly help to solve it.请帮助解决它。 在此处输入图片说明

Here with the code for FragmentMe.kt:这里是 FragmentMe.kt 的代码:

class FragmentMe : Fragment() {

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,


               savedInstanceState: Bundle?): View? {

        recylerView_Main.layoutManager = LinearLayoutManager(context)
        recylerView_Main.adapter = Mainadapter()

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_me, container, false)
    }

}

Here with the code of MainActivity.kt:这里有 MainActivity.kt 的代码:

class MainActivity : AppCompatActivity() {

    val manager = supportFragmentManager

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                //message.setText(R.string.title_home)
                createFragmentQpon()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                //message.setText(R.string.title_dashboard)
                createFragmentMe()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                //message.setText(R.string.title_notifications)
                createFragmentTools()
                return@OnNavigationItemSelectedListener true
            }

        }
        false
    }

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

        //Action Bar
        val actionBar = supportActionBar
        actionBar!!.setDisplayShowHomeEnabled(true)
        actionBar.setBackgroundDrawable(ColorDrawable(Color.parseColor("#00FFFFFF")))
        actionBar.setIcon(R.drawable.ic_home_black_24dp)
        actionBar.setDisplayShowTitleEnabled(false)

        createFragmentQpon()
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    }

    fun createFragmentQpon() {
        val transaction = manager.beginTransaction()
        val fragment = FragmentQpon()
        transaction.replace(R.id.fragmentholder,fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createFragmentMe() {
        val transaction = manager.beginTransaction()
        val fragment = FragmentMe()
        transaction.replace(R.id.fragmentholder,fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createFragmentTools() {
        val transaction = manager.beginTransaction()
        val fragment = FragmentTools()
        transaction.replace(R.id.fragmentholder,fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }



}

Here with the code of Mainadapter.kt:这里有 Mainadapter.kt 的代码:

class Mainadapter: RecyclerView.Adapter<CustomViewHolder>() {

    val videolist = listOf("aaa","bbbb","cccc")

    override fun getItemCount(): Int {
        return  3
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {

        val layoutInflater = LayoutInflater.from(parent?.context)
        val cellForRow = layoutInflater.inflate(R.layout.tutorial_layout, parent, false)
        return CustomViewHolder(cellForRow)

    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {

        var videoName = videolist.get(position)


        holder.itemView.title.text = videoName

    }


}

class CustomViewHolder(v: View): RecyclerView.ViewHolder(v) {
}

Move this code移动这个代码

recylerView_Main.layoutManager = LinearLayoutManager(context)
recylerView_Main.adapter = Mainadapter()

from onCreateView to onActivityCreatedonCreateViewonActivityCreated

override onActivityCreated and place the above code.覆盖onActivityCreated并放置上面的代码。

There are two things incorrect in your code :您的代码中有两件事不正确:

  1. You are trying to access recyclerView even before inflating the View.您甚至在膨胀视图之前就尝试访问recyclerView

  2. The context of a Fragment is null in onCreateView and is usable in between onAttach and onDetach Fragment 的上下文在 onCreateView 中为 null,可在onAttachonDetach之间使用

recylerView_Main.layoutManager = LinearLayoutManager(this.context)

试试这个,对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM