Android Fragments 碎片没有附着到Activity上,如何解决?

mec1mxoz  于 5个月前  发布在  Android
关注(0)|答案(3)|浏览(77)

我正在使用底部导航视图的导航组件。

问题是当我打开应用程序时,所选片段是主片段,然后当我导航到另一个片段(Favorite)并使用上下文时,一切正常。但是当我导航到主片段,然后导航到Favorite片段时,当我使用上下文时,应用程序崩溃,我得到此illegalStateException:java.lang.IllegalStateException: Fragment Favorite{19d069d} (bfafcdd4-bba2-4005-9437-65c1d0a51977) not attached to Activity

这是behavior
下面是我如何用navController初始化底部的导航

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

 setupWithNavController(binding.bottomNavigationView, navController)

字符串
这是一个nav文件:

<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/main_navigation"
    app:startDestination="@id/home">

    <fragment
        android:id="@+id/home"
        android:name="com.androdevelopment.wallpaper.view.home.Home"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/action_home_to_menu"
            app:destination="@id/menu" />
        <action
            android:id="@+id/action_home_to_favorite"
            app:destination="@id/favorite"
             />
    </fragment>
    <fragment
        android:id="@+id/favorite"
        android:name="com.androdevelopment.wallpaper.view.favorite.Favorite"
        android:label="fragment_favorite"
        tools:layout="@layout/fragment_favorite">
        <action
            android:id="@+id/action_favorite_to_home"
            app:destination="@id/home" />
        <action
            android:id="@+id/action_favorite_to_menu"
            app:destination="@id/menu"

            />
    </fragment>
    </navigation>


完整的日志:

java.lang.IllegalStateException: Fragment Favorite{9fbcc19} (f8c928e4-bc35-4543-a26b-f917feb3a9df) not attached to Activity
    at androidx.fragment.app.Fragment.startActivity(Fragment.java:1446)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1436)
at com.androdevelopment.wallpaper.view.favorite.Favorite$collectUiEvents$1$1.emit(Favorite.kt:245)
at com.androdevelopment.wallpaper.view.favorite.Favorite$collectUiEvents$1$1.emit(Favorite.kt:235)
at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAllImpl$FlowKt__ChannelsKt(Channels.kt:37)
at kotlinx.coroutines.flow.FlowKt__ChannelsKt.access$emitAllImpl$FlowKt__ChannelsKt(Channels.kt:1)
at kotlinx.coroutines.flow.FlowKt__ChannelsKt$emitAllImpl$1.invokeSuspend(Unknown Source:14)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:68)
atkotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:375)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
at com.androdevelopment.wallpaper.view.favorite.viewmodel.FavoritesViewModel.sendUiEvent(FavoritesViewModel.kt:97)
at com.androdevelopment.wallpaper.view.favorite.viewmodel.FavoritesViewModel.onEvent(FavoritesViewModel.kt:90)
at com.androdevelopment.wallpaper.view.favorite.Favorite$favoriteAdapter$2$1.onWallpaperClick(Favorite.kt:61)
at com.androdevelopment.wallpaper.view.favorite.adapter.FavoriteAdapter.onBindViewHolder$lambda$0(FavoriteAdapter.kt:48)
at com.androdevelopment.wallpaper.view.favorite.adapter.FavoriteAdapter.$r8$lambda$QHKf0dj1K1b9eNHLFJzLaOqv-q0(Unknown Source:0)
at com.androdevelopment.wallpaper.view.favorite.adapter.FavoriteAdapter$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7659)
at android.view.View.performClickInternal(View.java:7636)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:30156)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@b53a2bb, Dispatchers.Main.immediate]

2sbarzqh

2sbarzqh1#

您可以使用isAdded方法检查片段是否已附加到Activity。
简单地检查它像这样,做你的东西:

if(isAdded){
    // Fragment is attached.
}

字符串

q8l4jmvw

q8l4jmvw2#

请确保菜单中的id与导航中的id匹配。

也尝试在菜单中的id中添加或删除(+)。
菜单XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@id/home"
// some code
/>

<item android:id="@id/favorite"
//some code
/>

</menu>

字符串
导航XML:

<navigation
//some code
android:id="@+id/main_navigation"
app:startDestination="@id/home">

<fragment
    android:id="@+id/home"
        // some code
/>
</fragment>
<fragment
    android:id="@+id/favorite"
        // some code
/>
    
</navigation>

ilmyapht

ilmyapht3#

获取片段中的Activity并检查Activity是否为null

Activity activity = getActivity();
if (!isAdded() && activity == null) {
        return;
    }

字符串

相关问题