supportFragmentManager.findfragmentById返回null

发布于 2025-02-07 09:27:36 字数 1552 浏览 3 评论 0原文

当活动是主应用程序的一部分时,我在下面发布的代码工作。但是,当我将该项目(活动是该项目的一部分)导入到另一个项目中时,并通过startActivitiy()来调用活动。

我的活动具有以下代码 -

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navHostFragment =
      supportFragmentManager.findFragmentById(R.id.wlimpl_main_activity_nav_fragment)  as NavHostFragment
    navController = navHostFragment.navController
    }

supportfragmentManager.findfragmentById(r.id.wimpl_main_activity_nav_fragment) 正在返回空。

XML-

<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
    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/wlimpl_main_activity_nav_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_space_white"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/wlimpl_auth_nav_graph"
    tools:context=".MainActivity" />

我在做什么错?

编辑:

通过supportFragmentManager.Fragments显示0个片段。

The code I have posted below works when the activity is part of main app. However, when I import the project (of which the activity is a part of) as a library into another project and call the activity via startActivitiy() I have the null issue.

My activity has the following code -

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navHostFragment =
      supportFragmentManager.findFragmentById(R.id.wlimpl_main_activity_nav_fragment)  as NavHostFragment
    navController = navHostFragment.navController
    }

Here, supportFragmentManager.findFragmentById(R.id.wlimpl_main_activity_nav_fragment)
is returning null.

XML -

<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
    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/wlimpl_main_activity_nav_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_space_white"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/wlimpl_auth_nav_graph"
    tools:context=".MainActivity" />

What am I doing wrong?

Edit:

Checking via supportFragmentManager.fragments shows 0 fragments.

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

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

发布评论

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

评论(1

女皇必胜 2025-02-14 09:27:36

事实证明activity_main在库模块中与activity_main在主应用程序中发生冲突。我必须重命名XML文件之一。

Turns out activity_main in library module was conflicting with activity_main in the main app. I had to rename one of the xml files.

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