Kotlin Android Fragment recyclerView和上下文问题

时间:2018-05-19 16:15:39

标签: android android-recyclerview kotlin fragment android-context

我想在片段中创建一个recyclerView,但它显示错误" java.lang.IllegalStateException:recylerView_Main不能为null         在com.gph.bottomnavigation.FragmentMe.onCreateView(FragmentMe.kt:28)"

  • 问题1)请帮忙解决这个问题。
  • 问题2)我在一个没有任何片段的空项目中创建了一个recyclerView,它运行正常。

enter image description here

但是相同的代码在Fragment中不起作用,它显示错误,所以我改变了" recylerView_Main.layoutManager = LinearLayoutManager(this)" to" recylerView_Main.layoutManager = LinearLayoutManager(context)"  它显示没有错误,我可以在simlulator中运行,但当我单击Fragment的导航按钮时,应用程序停止并显示此错误。请帮助解决它。 enter image description here

这里有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)
    }

}

这里有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()
    }



}

这里有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) {
}

2 个答案:

答案 0 :(得分:10)

移动此代码

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

onCreateViewonActivityCreated

覆盖onActivityCreated并放置上述代码。

您的代码中有两件事不正确:

  1. 您甚至在充气视图之前尝试访问recyclerView

  2. 片段的上下文在onCreateView中为null,可在onAttachonDetach之间使用

答案 1 :(得分:0)

recylerView_Main.layoutManager = LinearLayoutManager(this.context)

尝试一下,对我来说很好。