gpt4 book ai didi

android - 为什么在导航 Controller 中实现包含时出现 StackOverflowError?

转载 作者:行者123 更新时间:2023-12-05 00:09:17 24 4
gpt4 key购买 nike

我的项目文件:https://drive.google.com/file/d/11llz7ylWe7ACyLMBbqp6YzugUL8hhImt/view?usp=sharing

所以我有 2 个导航图。称为主导航图和身份验证图。

我将主图包含在 auth graph 中,反之亦然,auth graph 在主图中。

我想实现登录系统,所以当用户成功登录时,用户将进入主要 Activity (具有底部导航 View 和工具栏),身份验证 Activity 没有底部导航 View 或 fragment 。这是图表

  • 主导航图:

  • enter image description here
    <?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_graph"
    app:startDestination="@id/destination_home">

    <include app:graph="@navigation/auth_graph" />

    <fragment android:id="@+id/destination_home" android:name="com.muchammadagunglaksana.navcontroller.HomeFragment"
    android:label="Home Judul" tools:layout="@layout/fragment_home">
    <action android:id="@+id/action_toAuthActivity" app:destination="@id/auth_graph"/>
    </fragment>


    <fragment android:id="@+id/destination_camera" android:name="com.muchammadagunglaksana.navcontroller.CameraFragment"
    android:label="Camera Judul" tools:layout="@layout/fragment_camera">
    <action android:id="@+id/toPhotosDestination" app:destination="@id/destination_photos"/>
    </fragment>


    <fragment android:id="@+id/destination_photos" android:name="com.muchammadagunglaksana.navcontroller.PhotosFragment"
    android:label="Foto Judul" tools:layout="@layout/fragment_photos">
    <action android:id="@+id/toHomeDestination" app:destination="@id/destination_home"/>
    <argument android:name="numberOfPhotos" app:argType="integer" android:defaultValue="0"/>
    </fragment>


    <fragment android:id="@+id/destination_settings"
    android:name="com.muchammadagunglaksana.navcontroller.SettingsFragment"
    android:label="Setting Judul" tools:layout="@layout/fragment_settings"/>
    </navigation>
  • 授权图:
    enter image description here

    <include app:graph="@navigation/navigation_graph" />

    <fragment android:id="@+id/loginFragment" android:name="com.muchammadagunglaksana.navcontroller.LoginFragment"
    android:label="fragment_login" tools:layout="@layout/fragment_login">
    <action android:id="@+id/action_toMainActivity" app:destination="@id/navigation_graph"/>
    </fragment>


  • 在 LoginFragment 中单击登录按钮时,我使用以下代码:
           login_button.setOnClickListener {
    Navigation.findNavController(it).navigate(R.id.action_toMainActivity)
    }

    并且在 HomeFragment 中,当单击注销按钮时,我使用:
    logout_button.setOnClickListener {
    Navigation.findNavController(it).navigate(R.id.action_toAuthActivity)
    }

    但我得到了stackoverflow错误:

    E/AndroidRuntime: FATAL EXCEPTION: main Process: com.muchammadagunglaksana.navcontroller, PID: 14322 java.lang.StackOverflowError: stack size 8MB at android.support.v4.util.SparseArrayCompat.(SparseArrayCompat.java:77) at android.support.v4.util.SparseArrayCompat.(SparseArrayCompat.java:62) at androidx.navigation.NavGraph.(NavGraph.java:44) at androidx.navigation.NavGraphNavigator.createDestination(NavGraphNavigator.java:54) at androidx.navigation.NavGraphNavigator.createDestination(NavGraphNavigator.java:29) at androidx.navigation.NavInflater.inflate(NavInflater.java:100) at androidx.navigation.NavInflater.inflate(NavInflater.java:80) at androidx.navigation.NavInflater.inflate(NavInflater.java:128) at androidx.navigation.NavInflater.inflate(NavInflater.java:80) at androidx.navigation.NavInflater.inflate(NavInflater.java:128) at androidx.navigation.NavInflater.inflate(NavInflater.java:80) at androidx.navigation.NavInflater.inflate(NavInflater.java:128) at androidx.navigation.NavInflater.inflate(NavInflater.java:80) at androidx.navigation.NavInflater.inflate(NavInflater.java:128) at androidx.navigation.NavInflater.inflate(NavInflater.java:80) at androidx.navigation.NavInflater.inflate(NavInflater.java:128)

    na.navcontroller E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!



    什么地方出了错 ?

    最佳答案

    一个 <include>标签完全等同于复制/粘贴包含图的确切内容来代替 <include> .通过拥有您的auth_graph包括 navigation_graph ,你已经建立了一个循环:navigation_graph包含 auth_graph其中包含 navigation_graph永远持续下去。

    您需要做的是删除 <include app:graph="@navigation/navigation_graph" />来自您的auth_graph .因为您的auth_graph已经在 navigation_graph 内,您无需再次添加它,但您可以直接引用任何这些目的地。

    关于android - 为什么在导航 Controller 中实现包含时出现 StackOverflowError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509821/

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