问题
我想在片段中创建一个 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 的代码:
- 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()
复制代码
从 onCreateView 到 onActivityCreated
覆盖 onActivityCreated 并放置上面的代码。
代码中有两个错误:
回收站查看
附加
分离
|