Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android视图绑定:从包含的xml布局访问NavHostFragment时_Android_Layout_Navigation_Include_Fragment - Fatal编程技术网

Android视图绑定:从包含的xml布局访问NavHostFragment时

Android视图绑定:从包含的xml布局访问NavHostFragment时,android,layout,navigation,include,fragment,Android,Layout,Navigation,Include,Fragment,我有activity_main.xml,启用了ViewBinding可以生成ActivityMainBinding类。我有另一个content_main.xml布局,它包含在该布局中,如下所示: activity_main.xml <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

我有activity_main.xml,启用了
ViewBinding
可以生成
ActivityMainBinding
类。我有另一个content_main.xml布局,它包含在该布局中,如下所示:

activity_main.xml

 <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_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

        <include layout="@layout/content_main"
            android:id="@+id/main_container"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

更新:这在使用FragmentContainerView时有效。

使用findNavController方法访问导航图。由于延迟了一些可见性,请尝试使用可运行的方法
Handler().postDelayed

要承载导航图,应使用
FragmentContainerView
而不是
Fragment
,并将name属性设置为:

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

谢谢你的回答!我将尝试使用处理程序,但我的问题更具体到“视图绑定”。因为没有足够的声誉,我不能使用标签。请点击此链接:您想访问androidx.navigation.fragment.NavHostFragment的目的是什么?你可以通过它的id+id“nav_host”来访问它是的,我做了,它被修复了。我应该把答案写在上面。我更新了问题本身!
 binding.apply {
            //Works
            mainContainer.textView.text = "test"
            //throws "Cannot access 'android.widget.fragment'. Check your module classpath for missing or conflicting dependencies"
            mainContainer.navHost.postDelayed({
                binding.mainContainer.navHost.systemUiVisibility = FLAGS_FULLSCREEN
            }, IMMERSIVE_FLAG_TIMEOUT)
        }
android:name="androidx.navigation.fragment.NavHostFragment"