Android - 使用带有活动和片段的导航 - 二进制 XML 文件错误 [英] Android - Use of navigation with activities and fragments - Binary XML file Error

查看:39
本文介绍了Android - 使用带有活动和片段的导航 - 二进制 XML 文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它由一个 Activity 和多个片段组成,这是 Google 推荐的.

I have an application composed of one activity and several fragments, as recommanded by Google.

- MainActivity
  - LobbyFragment
  - GameFragment
  - ...

我也想用导航图

我在Activity里放了一个菜单到处都有,然后我想切换页面的内容.

I put a menu in the Activity to have everywhere, and then I would like to switch the content of the page.

要按照 fragment 教程 中所示执行此操作,我制作了 activitymain.xml 以便稍后在片段之间切换 FragmentContainerView 的内容.

To do this as shown in the fragment tutorial, i made the activitymain.xml to later switch the content of the FragmentContainerView between fragments.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:theme="@style/Theme.application.AppBarOverlay">
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.my.application.LobbyFragment" /> <!--Here is failure-->

</androidx.coordinatorlayout.widget.CoordinatorLayout>

但是,在这一点上,当我编译时,我有:无法启动活动 ComponentInfo{com.my.application/com.my.application.MainActivity}:android.view.InflateException:二进制 XML 文件第 21 行:二进制 XML 文件第 21 行:错误膨胀类 androidx.fragment.app.FragmentContainerView

But, at this point, when I compile, I have : Unable to start activity ComponentInfo{com.my.application/com.my.application.MainActivity}: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class androidx.fragment.app.FragmentContainerView

以及将 Activity 放在导航地图中的哪个位置?

And where to put the Activity in the navigation map ?

推荐答案

文档链接 你引用的通过直接访问supportFragmentManager来使用传统的fragment事务;但从您的帖子中可以看出,您使用的是 导航架构组件.

The documentation link you referenced uses the traditional fragment transaction by directly accessing supportFragmentManager; but it turns out from your post that you use Navigation Architecture components instead.

在导航组件中,您不应该在 android:name 属性中使用特定的片段类,而是由 navController 使用 NavHostFragment 来处理

In navigation components, you are not supposed to use a particular fragment class in the android:name property, instead the navController takes care of that using NavHostFragment

要解决这个问题,请更换

To, fix this, please replace

android:name="com.my.application.LobbyFragment" 

android:name="androidx.navigation.fragment.NavHostFragment"

此外,您需要使用 app:navGraph 属性引用导航图的名称;假设它被命名为 nav_graph:

Also, you need to reference the name of the navGraph in that with app:navGraph attribute; assuming it's named as nav_graph:

app:navGraph="@navigation/nav_graph"

这篇关于Android - 使用带有活动和片段的导航 - 二进制 XML 文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆