Loading [MathJax]/jax/output/CommonHTML/config.js
社区首页 >问答首页 >带有FragmentContainerView的Null NavHostFragment/NavController

带有FragmentContainerView的Null NavHostFragment/NavController
EN

Stack Overflow用户
提问于 2020-05-15 12:18:02
回答 2查看 4.8K关注 0票数 3

期望的

使用导航UI的BottomNavigationViewFragmentContainerView创建底部的应用程序栏导航,类似于示例应用程序https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample

注意,NavigationAndvancedSample应用程序似乎没有使用与文档中描述的实现相同的实现,因为它使用自定义扩展方法setupWithNavController提供了解决方案。

观察到的

当使用一个NavHostFragment在MainActivity.kt中创建BottomNavigationView时,空FragmentContainerView

可在 https://github.com/AdamSHurwitz/BottomNavigationViewSample 存储库中获得可重复的错误和完整的代码示例。

错误

2020年-05-15 12:39:19.117 18747-18747/com.example.bottomnavigationviewsample E/AndroidRuntime:致命异常:主要进程: com.example.bottomnavigationviewsample,PID: 18747 java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.bottomnavigationviewsample/com.example.bottomnavigationviewsample.MainActivity}:kotlin.TypeCastException:空不能转换为非空类型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)在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 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) at java.lang.reflect.Method.invoke(本机方法),由: kotlin.TypeCastException: null引起,不能在com.example.bottomnavigationviewsample.MainActivity.onCreate(MainActivity.kt:16)转换为非空类型androidx.navigation.fragment.NavHostFragment。在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)在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),在安卓。App.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(本地方法)在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:发送信号。PID: 18747 SIG: 9

实现

  1. 为第一个视图home.xml创建导航图,以便在app > res >导航下的BottomNavigationView中显示。 当提示添加项目依赖项时,选择OK。 kotlinOptions { jvmTarget = '1.8' }添加到build.gradle (:someAppModule)中,以启用AppBarConfiguration的内联字节码。 向home.xml中添加一个片段,以便在BottomNavigationView中显示。

home.xml

代码语言:javascript
代码运行次数:0
复制
<?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

代码语言:javascript
代码运行次数:0
复制
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. BottomNavigationView,bottom_nav.xml,在app > res >菜单下创建一个菜单。 为菜单idtitle添加字符串值。 为菜单icon添加可绘制的矢量。

bottom_nav.xml

代码语言:javascript
代码运行次数:0
复制
<?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.kt的activity_main.xml布局中。

activity_main.xml

代码语言:javascript
代码运行次数:0
复制
<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. BottomNavigationView中启用MainActivity.kt。 a.创建AppBarConfiguration。参见:用NaviationUI > https://developer.android.com/guide/navigation/navigation-ui#appbarconfiguration更新UI组件 b.创建NavHostFragmentNavController。参见: StackOverflow https://stackoverflow.com/a/59275182/2253682解决方案 设置操作栏导航。 d.设置BottomNavigationView导航。参见:用NavigationUI > https://developer.android.com/guide/navigation/navigation-ui#bottom_navigation更新UI组件

MainActivity.kt

代码语言:javascript
代码运行次数:0
复制
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. NavController中所概述的那样,使用findNavController(R.id.nav_host_container)创建文档
  2. fragment中实现一个FragmentContainerView视图而不是FragmentContainerView视图。

activity_main.xml

代码语言:javascript
代码运行次数:0
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2020-10-21 07:34:12

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

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

代码语言:javascript
代码运行次数:0
复制
<navigation
android:id="@+id/home" ... >

并在菜单文件( menu /底部_Nav.xml)中:

代码语言:javascript
代码运行次数:0
复制
 <item
    android:id="@+id/home" ... />
票数 2
EN

Stack Overflow用户

发布于 2020-05-15 13:53:00

BottomNavigationView菜单项添加父导航图。

完整的示例代码包含在BottomNavigationSample中。

  1. 为菜单片段创建父级导航图。注意,这没有反映在NavigationAdvancedSample体系结构模式中,它有三个单独的导航图而没有父导航图。

这可以通过每个菜单项的一个父导航图和片段或嵌套导航图来实现。嵌套导航图很好,因为每个子流都可以在嵌套图中组织。

main.xml

代码语言:javascript
代码运行次数:0
复制
<?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"
    android:id="@+id/main"
    app:startDestination="@id/home">
    <include app:graph="@navigation/home" />
    <include app:graph="@navigation/saved" />
</navigation>
  1. 使用navGraphFragmentContainerView中添加父图。

activity_main.xml

代码语言:javascript
代码运行次数:0
复制
<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:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main"/>

    <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. BottomNavigationView中启用MainActivity.kt。
代码语言:javascript
代码运行次数:0
复制
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, R.id.saved))

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

        setupActionBarWithNavController(navController, appBarConfiguration)
        bottom_nav.setupWithNavController(navController)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61827683

复制
相关文章
MySQL 子查询 嵌套查询
意思就是内层的select查到了(至少查到了一行)才进行查询,没有查到就不进行查询。
宁在春
2022/10/31
12.1K0
mysql中多表嵌套查询例子_mysql子查询嵌套规则
MySQl从4.11版后已经完全支持嵌套查询了,那么下面举些简单的嵌套查询的例子吧(源程序来自MySQL User Manual):
全栈程序员站长
2022/11/01
3.4K0
MySQL 嵌套查询_嵌套查询和嵌套结果的区别
where course.cno=sc.cno and course.cname=’数据库’ and grade>=80)[/code](3)查询计算机系最高成绩。
全栈程序员站长
2022/09/22
4.3K0
MySQL——优化嵌套查询和分页查询
嵌套查询(子查询)可以使用SELECT语句来创建一个单列的查询结果,然后把这个结果作为过滤条件用在另一个查询中。嵌套查询写起来简单,也容易理解。但是,有时候可以被更有效率的连接(JOIN)替代。
撸码那些事
2018/10/08
2.9K0
MySQL——优化嵌套查询和分页查询
mysql嵌套子查询的应用
sql语句中一个查询有时未必能满足需求,应对多表联查时就需要进行嵌套查询。嵌套查询的意思是,一个查询语句块可以嵌套在另外一个查询块的where子句中,称为嵌套查询。其中外层查询也称为父查询,主查询。内层查询也称子查询,从查询。 嵌套查询的工作方式是:先处理内查询,由内向外处理,外层查询利用内层查询的结果嵌套查询不仅仅可以用于父查询select语句使用。还可以用于insert、update、delete语句或其他子查询中。
OECOM
2020/07/01
4.2K0
嵌套查询效率_sql嵌套查询例子
嵌套查询是 SQL 中表达能力很强的一种机制,既给应用带来了方便也给查询优化带来了很大的挑战。本文总结一下经典的单机系统对嵌套查询的优化。
全栈程序员站长
2022/09/27
2.4K0
sql server嵌套查询实验_exists嵌套查询
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/169426.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/22
1.8K0
sql的嵌套查询_嵌套查询和嵌套结果的区别
SQL连接查询和嵌套查询详解 连接查询 若一个查询同时涉及两个或两个以上的表,则称之为连接查询。连接查询是数据库中最最要的查询,
全栈程序员站长
2022/09/22
3.9K0
sql的嵌套查询_嵌套查询和嵌套结果的区别
mysql DATE_SUB() 函数
SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 5 DAY) AS SubtractDate FROM Orders
DencyCheng
2019/03/05
1.8K0
sql的嵌套查询_sql子查询嵌套优化
最近在做各类小应用,用到了MYSQL,有时候会用到一些比较复杂的嵌套查询,在研究怎么通过SQL实现这些。 假设下面这张表(stu)描述学生的基本信息:
全栈程序员站长
2022/09/22
5.2K0
sql嵌套查询和连接查询_sql子查询嵌套规则
WHERE department_id=( SELECT department_id
全栈程序员站长
2022/11/07
4K0
sql数据库嵌套查询_select嵌套查询
where 学号 = (select 学号 from 学生 where 姓名=”xx”);
全栈程序员站长
2022/09/22
3.8K0
sql嵌套查询例子_sql的多表数据嵌套查询
查询学生上课人数超过 “Eastern Heretic” 的任意一门课的学生人数的课程信息,请使用 ANY 操作符实现多行子查询。(Lintcode刷题记录)
全栈程序员站长
2022/09/22
3.1K0
SQL嵌套查询_sql嵌套查询返回多个字段
说到嵌套查询,首先得理解嵌套查询是什么意思,简单来说就是,一个查询语句可以嵌套在另外一个查询语句的where子句中。外层的查询称为父查询(主查询),内层的查询称为子查询(从查询)。
全栈程序员站长
2022/09/22
2.9K0
SQL嵌套查询_sql差集嵌套
派生表就是一个由查询结果生成的临时表。他是在外部查询的 FROM 中定义的。派生表的存在范围只是在外部查询中,只要外部查询结束了,派生表也就不存在了。派生表一定要写在 FROM 后面范围内,用()括起来。后面跟着派生表的名称。
全栈程序员站长
2022/09/22
2.2K0
SQL嵌套查询_sql差集嵌套
sql嵌套查询效率_sql嵌套查询返回多个字段
为了查询一个字段,使用了五层嵌套循环,但是花费了约1分钟 但是5个表的数据每个最多只有10条,怎么会这么慢呢?
全栈程序员站长
2022/09/22
2.8K0
sql嵌套查询效率_sql嵌套查询返回多个字段
Gorm-嵌套查询
嵌套查询是一种在一个查询语句中嵌套另一个查询语句的方式。在Gorm中,可以使用Preload方法来实现嵌套查询。
堕落飞鸟
2023/04/24
8950
sql中的嵌套查询_sql的多表数据嵌套查询
测试的时候发现取出的是一条数据, 因为测试的时候是一天中的两条数据, 没有不同的日期,所以当日以为是正确的 ,然而第二天写入数据了,要取出数据,却发现没有数据, 返回空的行, 以为都是代码又有问题 了,找了半天都没有 ,仔细看看了存储过程中的代码,发现这样返回的数据的确是空的。
全栈程序员站长
2022/09/22
7.1K0
实验3.4 嵌套查询
掌握SELECT语句的嵌套使用,实现多表的复杂查询,进一步理解SELECT语句的高级使用方法。
week
2018/08/27
8770
SELECT 语句中的 子查询(Sub Query)
子查询(Sub Query)或者说内查询(Inner Query),也可以称作嵌套查询(Nested Query),是一种嵌套在其他 SQL 查询的 WHERE 子句中的查询。
一个会写诗的程序员
2018/08/17
3.2K0

相似问题

MySQL SELECT IN with sub查询

21

Mysql使用update和sub查询

11

MYSQL insert after sub查询with count

30

DATE_SUB命名查询MYSQL

10

MYSQL查询Slow - Sub查询和临时表

21
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档