缺少带有 ID 的必需视图,导航图 android

Posted

技术标签:

【中文标题】缺少带有 ID 的必需视图,导航图 android【英文标题】:Missing required view with ID, navigation graph android 【发布时间】:2021-12-20 14:10:15 【问题描述】:

我的应用中有 2 个导航图 我有 2 个名称相同但 UI 不同的片段,并且也位于不同的包中。

现在我导航到它工作的一个片段,当我导航到另一个片段时,我得到了异常

java.lang.NullPointerException: Missing required view with ID: com.octave.staging:id/cashPieChart
    at com.octave.more.databinding.CashDistributionFragmentBinding.bind(CashDistributionFragmentBinding.java:104)
    at com.octave.more.databinding.CashDistributionFragmentBinding.inflate(CashDistributionFragmentBinding.java:65)
    at com.octave.more.cash.CashDistributionFragment.onCreateView(CashDistributionFragment.kt:34)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)

这些是我的导航图

<navigation 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:id="@+id/dashboard_nav_graph"
app:startDestination="@id/dashboardFragment">

<fragment
    android:id="@+id/dashboardFragment"
    android:name="com.octave.dashboard.DashboardFragment"
    android:label="dashboard_fragment"
    tools:layout="@layout/dashboard_fragment" >
    <action
        android:id="@+id/action_dashboardFragment_to_equityFragment"
        app:destination="@id/dashboardCashDistributionFragment" />
    <action
        android:id="@+id/action_dashboardFragment_to_cashDistributionFragment"
        app:destination="@id/dashboardEquityFragment" />
</fragment>

<fragment
    android:id="@+id/dashboardEquityFragment"
    android:name="com.octave.dashboard.equity.EquityFragment"
    tools:layout="@layout/equity_fragment"
    android:label="EquityFragment" />

<fragment
    android:id="@+id/dashboardCashDistributionFragment"
    android:name="com.octave.dashboard.cash.CashDistributionFragment"
    android:label="cash_distribution_fragment"
    tools:layout="@layout/cash_distribution_fragment" />
</navigation>

第二张图

<navigation 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:id="@+id/more_nav_graph"
app:startDestination="@id/moreFragment">

<fragment
    android:id="@+id/moreFragment"
    android:name="com.octave.more.MoreFragment"
    android:label="more_fragment"
    tools:layout="@layout/more_fragment">
    <action
        android:id="@+id/action_moreFragment_to_equityFragment"
        app:destination="@id/equityFragment" />
    <action
        android:id="@+id/action_moreFragment_to_cashDistributionFragment"
        app:destination="@id/cashDistributionFragment" />
   
</fragment>
<fragment
    android:id="@+id/equityFragment"
    android:name="com.octave.more.equity.EquityFragment"
    android:label="equity_fragment"
    tools:layout="@layout/equity_fragment" />
<fragment
    android:id="@+id/cashDistributionFragment"
    android:name="com.octave.more.cash.CashDistributionFragment"
    android:label="cash_distribution_fragment"
    tools:layout="@layout/cash_distribution_fragment" />

 </navigation>

导航代码如下

findNavController().navigate(DashboardFragmentDirections.actionDashboardFragmentToEquityFragment())

findNavController().navigate(MoreFragmentDirections.actionMoreFragmentToCashDistributionFragment())

两者都有不同的操作 ID 和位置。 似乎无法解决问题

【问题讨论】:

您是否在活动 XML 中使用已弃用的 &lt;fragment&gt; 标记?您应该始终使用FragmentContainerView 我添加了导航图 xml 的代码,而不是布局 xml 的代码。您所说的对于布局 xml 是正确的 【参考方案1】:

我的项目采用多模块方法 问题是由于视图绑定,因为两个布局 xml 也具有相同的名称并位于 2 个不同的模块中。

更改 1 个 xml 的名称后,它正在工作。

但是我没有得到相同的文件名存在于不同的包中,但视图绑定将其引用到第一个。

【讨论】:

以上是关于缺少带有 ID 的必需视图,导航图 android的主要内容,如果未能解决你的问题,请参考以下文章

每个底部导航视图菜单项的单独导航图 - Android

在 Swift 中缺少带有导航控制器的视图控制器中的后退按钮

带有圆形按钮的Android底部导航

Android导航图继续api调用问题

从搜索控制器导航后缺少导航栏

导航控制器中的 UIViewController 缺少返回按钮