使用类似于示例应用程序BottomNavigationView
的导航UI的FragmentContainerView
和NavigationAdavancedSample创建底部应用程序栏导航。
注意,NavigationAndvancedSample应用程序似乎没有使用documentation中所述的相同实现,因为它具有使用自定义扩展方法setupWithNavController
的解决方法。
NavHostFragment
创建BottomNavigationView
时,[[Null FragmentContainerView
。可再现的错误和完整的代码示例可在BottomNavigationViewSample存储库中找到。
kotlin.TypeCastException:无法将null强制转换为非null类型的androidx.navigation.fragment.NavHostFragment] >在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2016)在android.os.Handler.dispatchMessage(Handler.java:107)在android.os.Looper.loop(Looper.java:214)在android.app.ActivityThread.main(ActivityThread.java:7356)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)原因:kotlin.TypeCastException:无法将null强制转换为非null类型androidx.navigation.fragment.NavHostFragment在com.example.bottomnavigationviewsample.MainActivity.onCreate(MainActivity.kt:16)在android.app.Activity.performCreate(Activity.java:7802)在android.app.Activity.performCreate(Activity.java:7791)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2016)在android.os.Handler.dispatchMessage(Handler.java:107)在android.os.Looper.loop(Looper.java:214)在android.app.ActivityThread.main(ActivityThread.java:7356)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)2020-05-15 12:39:19.132 18747-18747 / com.example.bottomnavigationviewsample I / Process:正在发送信号。 PID:18747 SIG:9
实施
BottomNavigationView
中。a。当提示您输入添加项目依赖项时,选择确定
。b。将kotlinOptions { jvmTarget = '1.8' }
添加到build.gradle(:someAppModule)以启用AppBarConfiguration
的内联字节码。
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'
}
在[[app> res>菜单
BottomNavigationView
创建一个菜单,id
和title
添加字符串值。b。为菜单icon
添加可绘制的矢量。
<?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>
将FragmentContainerView
和BottomNavigationView
添加到MainActivity.kt
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>
在MainActivity.kt
BottomNavigationView
。AppBarConfiguration
。 b。创建NavHostFragment
和NavController
。 请参阅:FragmentContainerView using findNavController的StackOverflow解决方案>>
c。设置操作栏导航。
d。设置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)
}
}
尝试的解决方案
如
NavController
中所述用findNavController(R.id.nav_host_container)
创建documentation。
fragment
视图而不是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>
预计使用导航UI的BottomNavigationView和FragmentContainerView创建底部应用栏导航,类似于示例应用NavigationNavavanSample。注意,NavigationAndvancedSample ...
为
BottomNavigationView
菜单项添加父级导航图。