处理 android.view.InflateException:膨胀类片段时出错

发布于 2025-01-12 10:02:45 字数 2859 浏览 0 评论 0原文

我正在尝试构建一个具有托管多个片段的单个活动的应用程序。我还使用导航图和数据绑定。

我收到此错误,即使在查看 StackOverflow 上的类似问题后我也找不到解决方法。我得到的只是使用支持片段管理器和地图片段的旧方法,这对我的问题没有帮助。

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.uxstate.goldsearchkotlin/com.uxstate.goldsearchkotlin.MainActivity}:android.view.InflateException:com.uxstate.goldsearchkotlin 中的二进制 XML 文件第 9 行:layout /activity_main:com.uxstate.goldsearchkotlin 中的二进制 XML 文件第 9 行:layout/activity_main:错误膨胀类片段 引起原因: android.view.InflateException:com.uxstate.goldsearchkotlin 中的二进制 XML 文件第 9 行:layout/activity_main:com.uxstate.goldsearchkotlin 中的二进制 XML 文件第 9 行:layout/activity_main:膨胀类片段时出错 引起原因: android.view.InflateException:com.uxstate.goldsearchkotlin 中的二进制 XML 文件第 9 行:layout/activity_main:com.uxstate.goldsearchkotlin 中的二进制 XML 文件第 9 行:layout/activity_main:膨胀类片段时出错 导致:android.view.InflateException:com.uxstate.goldsearchkotlin 中的二进制 XML 文件第 9 行:layout/activity_main:膨胀类片段时出错

引起:kotlin.UninitializedPropertyAccessException:lateinit 属性适配器尚未初始化 在 com.uxstate.goldsearchkotlin.MainListFragment.getAdapter(MainListFragment.kt:23) 在 com.uxstate.goldsearchkotlin.MainListFragment.onViewCreated(MainListFragment.kt:57)

这是我的 NavHostFragment,这是崩溃的主要原因。

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

令人惊讶的是,如果我将 更改为 FrameLayout,应用程序不会崩溃,但布局无法与 FrameLayout 正常工作。

这就是我如何膨胀主页片段的布局。

class MainListFragment : Fragment() {
    private val viewModel: ListViewModel by activityViewModels()
    private var _binding: FragmentMainListBinding? = null
    private val binding get() = _binding!!
    lateinit var adapter: MainAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMainListBinding.inflate(inflater, container, false)
        val root: View = binding.root
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Add click listener to floating action button
        binding.fabMain.setOnClickListener {
            addListItem()
        }
        adapter = MainAdapter(viewModel.mainList)
        main_list_view.adapter = adapter
        main_list_view.layoutManager = LinearLayoutManager(requireContext())
    }

需要有关如何解决此异常的帮助。

I am trying to build an app with a single activity that hosts multiple fragments. I am also using Nav Graph and Data Binding.

I am getting this error which I can't find a way around it even after reviewing similar questions on StackOverflow. All I am getting is old ways about using support fragment manager and maps fragment which are not helping my issue.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uxstate.goldsearchkotlin/com.uxstate.goldsearchkotlin.MainActivity}: android.view.InflateException: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.uxstate.goldsearchkotlin:layout/activity_main: Error inflating class fragment

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property adapter has not been initialized
at com.uxstate.goldsearchkotlin.MainListFragment.getAdapter(MainListFragment.kt:23)
at com.uxstate.goldsearchkotlin.MainListFragment.onViewCreated(MainListFragment.kt:57)

This is my NavHostFragment which is the main cause of the crash.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

Surprisingly, if I change <fragment> to FrameLayout the app is not crashing but the layout is not working right with FrameLayout.

This is how I inflate the layout for the home fragment.

class MainListFragment : Fragment() {
    private val viewModel: ListViewModel by activityViewModels()
    private var _binding: FragmentMainListBinding? = null
    private val binding get() = _binding!!
    lateinit var adapter: MainAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMainListBinding.inflate(inflater, container, false)
        val root: View = binding.root
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Add click listener to floating action button
        binding.fabMain.setOnClickListener {
            addListItem()
        }
        adapter = MainAdapter(viewModel.mainList)
        main_list_view.adapter = adapter
        main_list_view.layoutManager = LinearLayoutManager(requireContext())
    }

Need help on how to address this exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2025-01-19 10:02:45

出现错误是因为我没有初始化 lateinit var adapter: MainAdapter

我在 onCreateView() 中初始化了它,并且应用程序没有崩溃。

The error came about because I had not initialized lateinit var adapter: MainAdapter

I initialized this inside onCreateView() and the app did not crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文