gpt4 book ai didi

android - Kotlin Android Fragment recyclerView 和上下文问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:38 26 4
gpt4 key购买 nike

我想在 fragment 中创建一个 recyclerView,但它显示错误“java.lang.IllegalStateException: recylerView_Main must not be null 在 com.gph.bottomnavigation.FragmentMe.onCreateView(FragmentMe.kt:28)”

  • 问题 1) 请帮助解决这个问题。
  • 问题 2) 我只在一个没有任何 fragment 的空项目中创建了一个 recyclerView,它工作正常。

enter image description here

但相同的代码在 Fragment 中不起作用,它显示错误所以我将“recylerView_Main.layoutManager = LinearLayoutManager(this)”更改为“recylerView_Main.layoutManager = LinearLayoutManager(context)” 它没有显示错误,我可以在模拟器中运行,但是当我单击 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) {
}

最佳答案

移动这段代码

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

onCreateViewonActivityCreated

覆盖 onActivityCreated 并放置上面的代码。

您的代码中有两处不正确:

  1. 您甚至在膨胀 View 之前就尝试访问 recyclerView

  2. Fragment 的上下文在 onCreateView 中为 null,可在 onAttachonDetach

    之间使用

关于android - Kotlin Android Fragment recyclerView 和上下文问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50427209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com