【问题标题】:Android Studio, override fun onOptionsItemSelected(...) not navigating to second fragment... error of does not have NavController setAndroid Studio,覆盖 fun onOptionsItemSelected(...) not navigating to second fragment... 错误没有设置 NavController
【发布时间】:2022-01-16 00:56:07
【问题描述】:

我的应用有两个片段和主要活动。我正在使用菜单栏图标从主要活动/第一个片段转到第二个片段。

问题是单击图标时发生致命异常,我收到以下错误:

java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{ccc58e7 V.E...... ........ 0,0-1080,1584} does not have a NavController set

错误在override fun onOptionsItemSelected(item: MenuItem): Boolean 函数中调用了一行。具体线路是:

view?.let { Navigation.findNavController(it).navigate(MainFragmentDirections.actionMainFragmentToNotesFragment3()) }

此代码取自另一个正在运行的应用程序,因此我不明白为什么会出现此错误。

我将不胜感激。

所有代码如下:

MainFragment.kt

package com.genauapps.cis2818_proj4_runningapp

import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import android.view.Menu
import android.view.MenuItem
import android.app.Activity
import android.util.Log

class MainFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        setHasOptionsMenu(true)

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater.inflate(R.menu.menu, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_add_workout -> {
                Log.i("test", "Button Clicked")
                view?.let { Navigation.findNavController(it).navigate(MainFragmentDirections.actionMainFragmentToNotesFragment3()) }
            }
        }
        return true
    }
}

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="10dp"
    tools:context=".MainFragment"
    android:name="androidx.navigation.fragment.MainFragment"
    >

    <TextView
        android:id="@+id/main_activity_tv_main_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:fontFamily="monospace"
        android:text="@string/text_view_main_title"
        android:textColor="@color/old_mauve"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/divider"
        android:layout_width="409dp"
        android:layout_height="1dp"
        android:layout_marginTop="8dp"
        android:background="?android:attr/listDivider"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_activity_tv_main_title" />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider" />
</androidx.constraintlayout.widget.ConstraintLayout>

fragment_notes.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="10dp"
    tools:context=".NotesFragment"
    android:name="androidx.navigation.fragment.MainFragment"
    >

    <TextView
        android:id="@+id/notes_fragment_tv_myworkout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"
        android:text="@string/text_view_myworkout"
        android:textSize="20sp"
        android:textStyle="bold|italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/notes_fragment_et_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:fontFamily="monospace"
        android:hint="@string/edit_text_time"
        android:importantForAutofill="no"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textColorHint="@color/black"
        android:textSize="34sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/notes_fragment_tv_myworkout"
        tools:ignore="TextContrastCheck" />

    <EditText
        android:id="@+id/notes_fragment_et_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:fontFamily="monospace"
        android:hint="@string/edit_text_date"
        android:importantForAutofill="no"
        android:inputType="date"
        android:minHeight="48dp"
        android:textColorHint="@color/black"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/notes_fragment_et_time" />

    <EditText
        android:id="@+id/notes_fragment_et_distance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:fontFamily="monospace"
        android:hint="@string/edit_text_distance"
        android:inputType="numberDecimal"
        android:textColorHint="@color/black"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/notes_fragment_et_date"
        android:importantForAutofill="no" />

    <EditText
        android:id="@+id/notes_fragment_et_notes"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:fontFamily="monospace"
        android:hint="@string/edit_text_notes"
        android:importantForAutofill="no"
        android:inputType="textCapSentences|textAutoCorrect"
        android:minHeight="48dp"
        android:textColor="@color/black"
        android:textColorHint="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/notes_fragment_et_distance" />

    <Button
        android:id="@+id/notes_fragment_bt_start_stop"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:backgroundTint="@color/y_in_mn_blue"
        android:fontFamily="monospace"
        android:text="@string/button_text"
        android:textAlignment="center"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/notes_fragment_et_notes" />

    <Button
        android:id="@+id/notes_fragment_bt_save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:backgroundTint="@color/y_in_mn_blue"
        android:fontFamily="monospace"
        android:text="@string/button_text_save"
        android:textAlignment="center"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/notes_fragment_bt_start_stop" />

</androidx.constraintlayout.widget.ConstraintLayout>

导航.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/navigation"
    app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.genauapps.cis2818_proj4_runningapp.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main" >
        <action
            android:id="@+id/action_mainFragment_to_notesFragment3"
            app:destination="@id/notesFragment3" />
    </fragment>
    <fragment
        android:id="@+id/notesFragment3"
        android:name="com.genauapps.cis2818_proj4_runningapp.NotesFragment"
        android:label="fragment_notes"
        tools:layout="@layout/fragment_notes" >
        <action
            android:id="@+id/action_notesFragment3_to_mainFragment"
            app:destination="@id/mainFragment" />
    </fragment>
</navigation>

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_add_workout"
        android:icon="@drawable/ic_baseline_run_circle_24"
        android:title="@string/menu_add_workout"
        app:showAsAction="ifRoom" />
</menu>

Logcat

2021-12-11 17:24:18.121 27715-27715/com.genauapps.cis2818_proj4_runningapp I/test: Button Clicked
2021-12-11 17:24:18.125 27715-27715/com.genauapps.cis2818_proj4_runningapp D/AndroidRuntime: Shutting down VM
2021-12-11 17:24:18.126 27715-27715/com.genauapps.cis2818_proj4_runningapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.genauapps.cis2818_proj4_runningapp, PID: 27715
    java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{ccc58e7 V.E...... ........ 0,0-1080,1584} does not have a NavController set
        at androidx.navigation.Navigation.findNavController(Navigation.java:84)
        at androidx.navigation.fragment.NavHostFragment.findNavController(NavHostFragment.java:120)
        at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
        at com.genauapps.cis2818_proj4_runningapp.MainFragment.onOptionsItemSelected(MainFragment.kt:56)
        at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:3154)
        at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected(FragmentManager.java:2937)
        at androidx.fragment.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:427)
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:334)
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:264)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.app.AppCompatDelegateImpl.onMenuItemSelected(AppCompatDelegateImpl.java:1185)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:975)
        at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625)
        at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

【问题讨论】:

  • 从您的错误消息来看,您似乎使用FragmentTransaction 直接将您的MainFragment 添加到您的活动中,而不是通过NavHostFragment。能否包含活动的onCreate() 和活动的布局 XML?

标签: android android-fragments android-navigation


【解决方案1】:

根据@ianhanniballake 的评论:

activity_main.xml 中,我将FragmentContainerView 更改为NavHostFragment 容器,它解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多