小编efo*_*foc的帖子

Android 导航组件:未找到 NavHostFragments 错误

我目前正在我的应用程序中实现导航组件,但似乎发现 NavHostFragment 无论我做什么都不起作用。

我已经尝试过重建、失效和重新启动、更改名称以及更新我的 Android Studio,但似乎没有任何效果。我认为这也是我尝试在 MainActivity.kt 文件中获取 NavController 时出错的原因,因为它返回 null。

安卓工作室 4.0.1 版

nav_graph.xml 图像

在此处输入图片说明

导航图.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/nav_graph"
    app:startDestination="@id/mainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.movieapp.ui.main.MainFragment"
        android:label="MainFragment">
        <action
            android:id="@+id/action_mainFragment_to_searchMovieFragment"
            app:destination="@id/searchMovieFragment" />
    </fragment>
    <fragment
        android:id="@+id/searchMovieFragment"
        android:name="com.example.movieapp.ui.search.SearchMovieFragment"
        android:label="SearchMovieFragment" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <include
        layout="@layout/appbar" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

主活动.kt

class MainActivity : AppCompatActivity() {

    private lateinit var toolbar: …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-jetpack android-jetpack-navigation

13
推荐指数
4
解决办法
4769
查看次数

Android:导航组件 - 如何将工具栏菜单连接到片段?

我一直试图将导航组件添加到我的应用程序中。特别是我的工具栏菜单项。

我一直在关注Android 网站上的导航迁移文档,但我对它的工作原理甚至设置它感到困惑。

这是一个单活动多片段架构。

应用程序将在 MainFragment 中启动,然后在点击工具栏中的菜单项(例如 Main Frag 到 Search Movie Frag)时启动。用户将能够从应用程序中的任何位置使用菜单项进行导航(例如,如果他们单击主页图标,则应将其发送到应用程序中任何位置的主屏幕表单。仍在考虑是否应该这样做)。

主要的是我不知道如何正确地将导航组件附加到菜单项。

应用程序

手机上显示的主要片段

导航图

Android Studio 上显示的导航图

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

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.movieapp.ui.main.MainFragment"
        android:label="MainFragment">
        <action
            android:id="@+id/action_mainFragment_to_searchMovieFragment"
            app:destination="@id/searchMovieFragment" />
    </fragment>
    <fragment
        android:id="@+id/searchMovieFragment"
        android:name="com.example.movieapp.ui.search.SearchMovieFragment"
        android:label="SearchMovieFragment" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <include
        layout="@layout/appbar"
        android:id="@+id/mainToolBar"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/mainToolBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private …
Run Code Online (Sandbox Code Playgroud)

android android-fragments kotlin android-jetpack android-architecture-navigation

4
推荐指数
1
解决办法
7093
查看次数

整数列可以为空吗?

我创建了一个默认的integer列,null但是当我输入空的双引号时,""它会出现以下错误:

ERROR: invalid input syntax for integer: ""
Run Code Online (Sandbox Code Playgroud)

integer列是否必须0

我使用Java将查询发送到数据库.

postgresql quotes null integer postgresql-9.3

3
推荐指数
2
解决办法
1万
查看次数

Android Kotlin:在 DAO 中使用“挂起”时出错 - 遵循 CodeLabs 指南

我一直在遵循 CodeLabs 的本指南,在本节中它显示了在 DAO 方法中使用“挂起”关键字,但是当我这样做时,我收到此错误:

error: To use Coroutine features, you must add `ktx` artifact from Room as a dependency. androidx.room:room-ktx:<version>
    public abstract java.lang.Object deleteAll(@org.jetbrains.annotations.NotNull()
    public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull()
                                     ^
Run Code Online (Sandbox Code Playgroud)

我在网上查了一下,但人们说你不能在 DAO 方法中使用协程。

我还在此处添加了此步骤之前的所有 build.gradle 导入。

android coroutine gradle kotlin kotlin-coroutines

3
推荐指数
1
解决办法
1549
查看次数