找回密码
 立即注册
相关推荐换一批
  1. 仿互站网JS弹窗页广告代码
  2. 自适应资源网站文字广告位代码
  3. JT/T 301-2019 公路交叉分类与编码规则
  4. SL 261-2017 湖泊代码
  5. QB/T 5273-2018 工业用缝纫机数字控制器功能代码数据格式
  6. GA 557.9-2005 互联网上网服务营业场所信息安全管理代码第9部分:规则动作代码
  7. GA 557.3-2005 互联网上网服务营业场所信息安全管理代码第3部分:审计级别代码
  8. GA 408.1-2003 道路交通违章管理信息代码第1部分:交通违章行为代码
  9. GA 398.7-2002 经济F罪案件管理信息代码第7部分:撤销案件原因代码
  10. GA 398.8-2002 经济F罪案件管理信息代码第8部分:统计立(破)案时间段代码
  11. GA 398.9-2002 经济F罪案件管理信息代码第9部分:F罪主体类型代码
  12. GA 398.2-2002 经济F罪案件管理信息代码第2部分:案件来源代码
  13. GA 398.3-2002 经济F罪案件管理信息代码第3部分:案件督办级别代码
  14. GA 398.4-2002 经济F罪案件管理信息代码第4部分:初查审核结果代码
  15. GA 398.5-2002 经济F罪案件管理信息代码第5部分:侦查工作阶段代码
  16. GA 398.6-2002 经济F罪案件管理信息代码第6部分:补立案件原因代码
  17. GA 777.2-2008 指纹数据代码第2部分:指纹纹型代码
  18. GA 777.3-2008 指纹数据代码第3部分:乳突线颜色代码
  19. GA 777.6-2008 指纹数据代码第6部分:指纹协查级别代码
  20. GA 690.1-2007 民用爆Z物品管理信息代码第1部分:民用爆Z物品品种分类与代码
  21. GA 777.4-2008 指纹数据代码第4部分:被捺印指纹人员类别代码
  22. YD/T 1962-2009 具有复用去复用功能的 10Gbit/s 和 40Gbit/s 光收发合一模块软件技?
  23. GA/T 852.4-2009 娱乐服务场所治安管理信息规范第4部分:娱乐服务场所变更类型代码
  24. GA/T 852.7-2009 娱乐服务场所治安管理信息规范第7部分:娱乐服务场所状态代码
问题
我想在片段中创建一个 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++ 连接器内存泄漏
下一篇:在图像上为缩小的圆圈设置动画