supportFragmentManager.findfragmentById返回null
当活动是主应用程序的一部分时,我在下面发布的代码工作。但是,当我将该项目(活动是该项目的一部分)导入到另一个项目中时,并通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明
activity_main
在库模块中与activity_main
在主应用程序中发生冲突。我必须重命名XML文件之一。Turns out
activity_main
in library module was conflicting withactivity_main
in the main app. I had to rename one of the xml files.