java.lang.ClassCastException:无法将片段转换为 androidx.fragment.app.Fragment

问题描述 投票:0回答:0

我正在为大学作业创建一个 todo kotlin 应用程序,在 Android 级别 33 上运行。当我尝试运行该应用程序时,我遇到了以下错误:

“java.lang.ClassCastException:com.leedsbeckett.todo_application.fragments.list.ListFragment 无法转换为 androidx.fragment.app.Fragment”

这里是ListFragment.kt的代码:

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.leedsbeckett.todo_application.R

class ListFragment : Fragment() {

    private lateinit var mTaskViewModel: TaskViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_list, container, false)

        // Recyclerview
        val adapter = ListAdapter()
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerview)
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(requireContext())

        // UserViewModel
        mTaskViewModel = ViewModelProvider(this).get(TaskViewModel::class.java)
        mTaskViewModel.readAllData.observe(viewLifecycleOwner, Observer { user ->
            adapter.setData(user)
        })

        view.findViewById<FloatingActionButton>(R.id.floatingActionButton).setOnClickListener {
            findNavController().navigate(R.id.action_listFragment_to_addFragment)
        }

        return view
    }

}

关于此事的任何相关问题都没有得到解答或已过时,我在这里错过了什么?

android android-studio kotlin android-fragments androidx
© www.soinside.com 2019 - 2024. All rights reserved.