Android导航组件未显示片段。

9
我正在尝试在现有应用程序中实现新的Android导航组件。 我有两个碎片,除了名称不同外完全相同。当我将startDestination设置为fragment2时,片段似乎正确显示。当startDestination设置为fragment1时,我看不到充气的视图,但是我确实看到“Fragment 1 created”吐司。
我做错了什么?
class Fragment1 : Fragment() {

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?): View? {
    Toast.makeText(context, "Fragment 1 created", Toast.LENGTH_LONG).show()
    return inflater.inflate(R.layout.fragment1, container, false)
  }

}

class Fragment2 : Fragment() {

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?): View? {
    Toast.makeText(context, "Fragment 2 created", Toast.LENGTH_LONG).show()
    return inflater.inflate(R.layout.fragment2, container, false)
  }


}

nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
    app:startDestination="@id/fragment1">

  <fragment
      android:id="@+id/fragment1"
      android:name="com.package.anotherpackage.ui.Fragment1"
      android:label="fragment1"
      tools:layout="@layout/fragment1"/>

  <fragment
      android:id="@+id/fragment2"
      android:name="com.package.anotherpackage.ui.Fragment2"
      android:label="fragment2"
      tools:layout="@layout/fragment2"/>
</navigation>

主活动布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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"
    android:background="@android:color/background_light"
    tools:context=".MainActivity">

  <fragment
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/nav_host_fragment"
      android:name="androidx.navigation.fragment.NavHostFragment"
      app:navGraph="@navigation/nav_graph"
      app:defaultNavHost="true"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      />

</FrameLayout>

Fragment1布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.Fragment1"
    >

  <!-- TODO: Update blank fragment layout -->
  <TextView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:text="fragment 1"
      />

</FrameLayout>

依赖项:

//Navigation
  def nav_version = "1.0.0-alpha05"

  implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
  implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"

  // optional - Test helpers
  androidTestImplementation "android.arch.navigation:navigation-testing-ktx:$nav_version"

1
如果你将Fragment1更改为充气R.layout.fragment2(仅供测试),那么Fragment2布局是否会与Fragment1 Toast消息一起出现?如果是这样,请包含你的R.layout.fragment1布局,它很可能是问题的原因。 - ianhanniballake
是的,我看到了Fragment2布局以及Fragment1的Toast。我编辑了帖子并添加了Fragment1布局。谢谢你的帮助! - Evan M
你的评论带领我找到了解决方案。我项目中的另一个布局(来自不同的库)与我在Fragment1中尝试加载的布局产生了冲突。再次感谢。 - Evan M
1个回答

4

在我项目的另一个库中有一个名为“fragment1.xml”的布局。 这可能与我尝试在Fragment1中填充的其他布局发生冲突。将Fragment1布局重命名后问题得到解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接