导航组件无法与绑定一起使用

问题描述 投票:0回答:1

我有一个名为 StartFragment 的片段。我想在 main_activity 布局中显示这个片段,如果在 MainActivity 中使用 viewBinding 片段将不会显示,如果我使用 setContentView(R.layout.activity_main) 设置布局效果很好。 我在 android studio Arctic-Fox 2020.3.1、Android Studio 4.2.1 和 Android Studio 4.0 中测试,结果是相同的片段未显示在 Activity_main

activity_main.xml:

 <androidx.constraintlayout.widget.ConstraintLayout
    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">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        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/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

主要活动:

    class MainActivity : AppCompatActivity() {
    private lateinit var mainBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //mainBinding = ActivityMainBinding.inflate(layoutInflater) this line does not show fragment
        setContentView(R.layout.activity_main) // Works fine
    }
}

导航图:

    <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/startFragment">

    <fragment
        android:id="@+id/startFragment"
        android:name="com.example.vilosoftgym.StartFragment"
        android:label="fragment_start"
        tools:layout="@layout/fragment_start" />
</navigation>

fragment_start.xml:

<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=".StartFragment">

    <Button
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/start_blank_fragment"
        android:text="Start Blank Fragment"/>

</FrameLayout>

开始片段:

class StartFragment : Fragment() {

    private var _binding: FragmentStartBinding? = null
    private val binding get() = _binding!!
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentStartBinding.inflate(layoutInflater, container, false)

        return binding.root
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }
}

构建.gradle:

 android {
    .
    .
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    .
    .

    // Navigation Component
    def nav_version = "2.3.5"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    // Feature module Support
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

}

提前致谢。

android android-studio navigation
1个回答
0
投票

您找到解决这个问题的方法了吗?我有完全相同的问题,并且已经尝试解决这个问题了大约几周。

© www.soinside.com 2019 - 2024. All rights reserved.