我目前在Jetpack Compose中遇到一个非常奇怪的问题。 我正在使用Navigation组件库(在NavHostFragment中托管Fragments)。 一些较新的Fragments已经在使用Compose,但偶尔会出现崩溃。 大多数情况是在使用导航抽屉导航到设置页面并在底部导航视图中点击返回项目时发生崩溃。 我目前使用的是 Jetpack Compose alpha-12 和 Navigation 2.3.0。
(使用普通的ViewBinding时不会崩溃)
@AndroidEntryPoint
class StatisticsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
TemporyTheme {
Text("Hello Compose!")
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
...>
<RelativeLayout
...>
<RelativeLayout
...>
<com.google.android.material.appbar.MaterialToolbar
... />
</RelativeLayout>
<!-- NavHost -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_main"
... />
<com.google.android.material.bottomnavigation.BottomNavigationView
... />
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
.../>
</androidx.drawerlayout.widget.DrawerLayout>
java.lang.NullPointerException: Attempt to invoke interface method 'void androidx.compose.runtime.MutableState.setValue(java.lang.Object)' on a null object reference
at androidx.compose.ui.platform.AndroidComposeView.setLayoutDirection(AndroidComposeView.kt:797)
at androidx.compose.ui.platform.AndroidComposeView.onRtlPropertiesChanged(AndroidComposeView.kt:724)
at android.view.View.resolvePadding(View.java:20272)
at android.view.ViewGroup.resolvePadding(ViewGroup.java:7765)
at android.view.View.initializeScrollbarsInternal(View.java:6781)
at android.view.View.<init>(View.java:6112)
at android.view.ViewGroup.<init>(ViewGroup.java:697)
at android.view.ViewGroup.<init>(ViewGroup.java:693)
at android.view.ViewGroup.<init>(ViewGroup.java:689)
at android.view.ViewGroup.<init>(ViewGroup.java:685)
at androidx.compose.ui.platform.AndroidComposeView.<init>(AndroidComposeView.kt:116)
at androidx.compose.ui.platform.WrapperKt.setContent(Wrapper.kt:123)
at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.kt:183)
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.kt:215)
at android.view.View.dispatchAttachedToWindow(View.java:20479)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
at android.view.ViewGroup.addViewInner(ViewGroup.java:5278)
at android.view.ViewGroup.addView(ViewGroup.java:5064)
at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.java:290)
at android.view.ViewGroup.addView(ViewGroup.java:5004)
at androidx.fragment.app.FragmentStateManager.addViewToContainer(FragmentStateManager.java:830)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:520)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2177)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2094)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1990)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
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:7660)
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)