[软件设计/软件工程] Kotlin Android 片段回收服务和上下文问题

[复制链接]
发表于 2022-5-6 13:07:37
问题
我想在片段中创建一个 recyclerView,但它显示错误“java.lang.IllegalStateException:recylerView_Main 不能为空

在 com.gph.bottomnavigation.FragmentMe.onCreateView (FragmentMe.kt:28) 上。 ”

但是相同的代码在片段中不起作用,它显示错误,所以我更改了“recylerView_Main.layoutManager=LinearLayoutManager(this)”到“recylerView_Main.layoutManager=LinearLayoutManager(context)”

它没有显示错误,我可以在模拟器中运行,但是当我单击片段的导航按钮时,应用程序停止并出现此错误。请帮忙。

这是 FragmentMe.kt 的代码:
  1. class FragmentMe : Fragment() {

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


  3.                savedInstanceState: Bundle?): View? {

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

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

  9. }
复制代码

这是 MainActivity.kt 的代码:
  1. class MainActivity : AppCompatActivity() {

  2.     val manager = supportFragmentManager

  3.     private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
  4.         when (item.itemId) {
  5.             R.id.navigation_home -> {
  6.                 //message.setText(R.string.title_home)
  7.                 createFragmentQpon()
  8.                 return@OnNavigationItemSelectedListener true
  9.             }
  10.             R.id.navigation_dashboard -> {
  11.                 //message.setText(R.string.title_dashboard)
  12.                 createFragmentMe()
  13.                 return@OnNavigationItemSelectedListener true
  14.             }
  15.             R.id.navigation_notifications -> {
  16.                 //message.setText(R.string.title_notifications)
  17.                 createFragmentTools()
  18.                 return@OnNavigationItemSelectedListener true
  19.             }

  20.         }
  21.         false
  22.     }

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

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

  32.         createFragmentQpon()
  33.         navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
  34.     }

  35.     fun createFragmentQpon() {
  36.         val transaction = manager.beginTransaction()
  37.         val fragment = FragmentQpon()
  38.         transaction.replace(R.id.fragmentholder,fragment)
  39.         transaction.addToBackStack(null)
  40.         transaction.commit()
  41.     }

  42.     fun createFragmentMe() {
  43.         val transaction = manager.beginTransaction()
  44.         val fragment = FragmentMe()
  45.         transaction.replace(R.id.fragmentholder,fragment)
  46.         transaction.addToBackStack(null)
  47.         transaction.commit()
  48.     }

  49.     fun createFragmentTools() {
  50.         val transaction = manager.beginTransaction()
  51.         val fragment = FragmentTools()
  52.         transaction.replace(R.id.fragmentholder,fragment)
  53.         transaction.addToBackStack(null)
  54.         transaction.commit()
  55.     }



  56. }
复制代码

这是 Mainadapter.kt 的代码:
  1. class Mainadapter: RecyclerView.Adapter<CustomViewHolder>() {

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

  3.     override fun getItemCount(): Int {
  4.         return  3
  5.     }

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

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

  10.     }

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

  12.         var videoName = videolist.get(position)


  13.         holder.itemView.title.text = videoName

  14.     }


  15. }

  16. class CustomViewHolder(v: View): RecyclerView.ViewHolder(v) {
  17. }
复制代码

回答
移动此代码
  1. recylerView_Main.layoutManager = LinearLayoutManager(context)
  2. recylerView_Main.adapter = Mainadapter()
复制代码

从 onCreateView 到 onActivityCreated

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

代码中有两个错误:

回收站查看

附加

分离





上一篇:MySQL C++ 连接器内存泄漏
下一篇:在图像上为缩小的圆圈设置动画

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表