gpt4 book ai didi

android - 带有 FragmentContainerView 的空 NavHostFragment/NavController

转载 作者:行者123 更新时间:2023-12-04 23:58:02 25 4
gpt4 key购买 nike

预计

使用类似于示例应用的导航 UI 的 BottomNavigationViewFragmentContainerView 创建底部应用栏导航 NavigationAdavancedSample

请注意,NavigationAndvancedSample 应用似乎未使用与 documentation 中描述的相同实现因为它有一个使用自定义扩展方法 setupWithNavController 的解决方法实现。

观察到

MainActivity.kt 中使用 FragmentContainerView 创建 BottomNavigationView 时为 Null NavHostFragment

BottomNavigationViewSample 中提供了可重现的错误和完整的代码示例存储库。

错误

2020-05-15 12:39:19.117 18747-18747/com.example.bottomnavigationviewsample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bottomnavigationviewsample, PID: 18747 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottomnavigationviewsample/com.example.bottomnavigationviewsample.MainActivity}: kotlin.TypeCastException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: kotlin.TypeCastException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment at com.example.bottomnavigationviewsample.MainActivity.onCreate(MainActivity.kt:16) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)  2020-05-15 12:39:19.132 18747-18747/com.example.bottomnavigationviewsample I/Process: Sending signal. PID: 18747 SIG: 9

实现

  1. 为第一个 View home.xml 创建一个导航图,以显示在 app > res > navigation 下的 BottomNavigationView.

    一个。当提示添加项目依赖项时,选择确定

    kotlinOptions { jvmTarget = '1.8' } 添加到 build.gradle (:someAppModule) 以启用 AppBarConfiguration 的内联字节码。

    将 fragment 添加到 home.xml 以显示在 BottomNavigationView 中。

home.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/home"
app:startDestination="@id/homeFragment">

<fragment
android:id="@+id/homeFragment"
android:name="com.example.bottomnavigationviewsample.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
</navigation>

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

kotlinOptions { jvmTarget = '1.8' }

defaultConfig {
applicationId "com.example.bottomnavigationviewsample"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
}

  1. app > res > menu 下为 BottomNavigationView 创建一个菜单,bottom_nav.xml

    一个。为菜单 idtitle 添加字符串值。

    为菜单 icon 添加矢量可绘制对象。

bottom_nav.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@drawable/ic_home_black_24dp"
android:contentDescription="@string/cd_home"
android:title="@string/title_home" />
</menu>
  1. FragmentContainerViewBottomNavigationView 添加到 MainActivity.ktactivity_main.xml 布局。

activity_main.xml

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav"/>
</LinearLayout>
  1. MainActivity.kt 中启用 BottomNavigationView

    一个。创建 AppBarConfiguration参见:使用 NaviationUI 更新 UI 组件 > AppBarConfiguration

    创建 NavHostFragmentNavController参见:FragmentContainerView using findNavController 的 StackOverflow 解决方案

    设置操作栏导航。

    设置 BottomNavigationView 导航。 请参阅:使用 NavigationUI 更新 UI 组件 > Bottom navigation

MainActivity.kt

package com.example.bottomnavigationviewsample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val appBarConfiguration = AppBarConfiguration(setOf(R.id.home))

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment
val navController = navHostFragment.navController

setupActionBarWithNavController(navController, appBarConfiguration)
bottom_nav.setupWithNavController(navController)
}
}

尝试的解决方案

  1. 使用 findNavController(R.id.nav_host_container) 创建 NavController,如 documentation 中所述.

  2. activity_main.xml 中实现 fragment View 而不是 FragmentContainerView

activity_main.xml

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">

<fragment
android:id="@+id/nav_host_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav"/>
</LinearLayout>

最佳答案

我也遇到过这个问题。我的错误是我没有在导航文件中输入正确的导航标签 ID。请注意,ID 必须等于菜单项的 ID。

在导航文件(navigation/home.xml)中:

<navigation
android:id="@+id/home" ... >

在菜单文件(menu/bottom_nav.xml)中:

 <item
android:id="@+id/home" ... />

关于android - 带有 FragmentContainerView 的空 NavHostFragment/NavController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61827683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com