gpt4 book ai didi

android - 为什么 fragment 的父级在暂停后为空?

转载 作者:行者123 更新时间:2023-12-05 04:26:25 24 4
gpt4 key购买 nike

当我运行应用程序并使用它时没有崩溃。 BottomSheetBehavior 工作正常。但是,如果我在后台长时间暂停后返回应用程序,当尝试将 view.parent as View 转换为 View 以查找具有 BottomSheetBehavior 的 View 时,它会因空点异常而崩溃.

为什么 fragment 暂停后没有父级?如何解决?

我尝试做 binding.root.parent as View 和其他 requireActivity().findViewById(R.id.fragment_sheet_container)。但同一案例的崩溃连续发生。

@AndroidEntryPoint
class ToolboxSheetFragment : Fragment(R.layout.fragment_toolbox_sheet) {
private lateinit var binding: FragmentToolboxSheetBinding
private lateinit var behavior: BottomSheetBehavior<View>

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding = FragmentToolboxSheetBinding.bind(view)
behavior = BottomSheetBehavior.from(
view.parent as View // null cannot be cast to non-null type android.view.View
)
}

fragment 的父级是下一个布局的主要 Activity :

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
tools:context=".view.MainActivity">

// ...

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_sheet_container"
android:name=".view.fragment.ToolboxSheetFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_hideable="false"
app:behavior_peekHeight="?actionBarSize"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
tools:layout="@layout/fragment_toolbox_sheet" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
java.lang.RuntimeException: Unable to start activity ComponentInfo{
.view.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type android.view.View
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2792)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: null cannot be cast to non-null type android.view.View
at .view.fragment.ToolboxSheetFragment.onViewCreated(ToolboxSheetFragment.kt:46)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3128)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:552)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1424)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2968)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2886)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:263)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:351)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1335)
at android.app.Activity.performStart(Activity.java:7043)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2755)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:172) 
at android.app.ActivityThread.main(ActivityThread.java:6590) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

最佳答案

如果能拿到完整的project就更好了。但我在这里建议几种方法:

方式一:

如何将行为 保留在 Activity 类中。实例化它 onCreate()。并像这样从 ToolboxSheetFragment 使用它。

class MainActivity : BaseActivity() {
var behavior: BottomSheetBehavior? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
behavior = BottomSheetBehavior.from(R.id.sheet_id)
}
}

然后从 ToolboxSheetFragment

class ToolboxSheetFragment : Fragment(R.layout.fragment_toolbox_sheet) {
private lateinit var binding: FragmentToolboxSheetBinding
var behavior: BottomSheetBehavior? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding = FragmentToolboxSheetBinding.bind(view)
behavior = (requireActivity() as MainActivity).behavior
}

方式二:

ToolboxSheetFragment 上使用 addOnAttachStateChangeListener:

@AndroidEntryPoint
class ToolboxSheetFragment : Fragment(R.layout.fragment_toolbox_sheet) {
private lateinit var binding: FragmentToolboxSheetBinding
private lateinit var behavior: BottomSheetBehavior<View>

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View?) {
if(v!=null){
binding = FragmentToolboxSheetBinding.bind(v)
behavior = BottomSheetBehavior.from(
v.parent as View
)
}
}

override fun onViewDetachedFromWindow(v: View?) {
view.removeOnAttachStateChangeListener(this)
}

})
}

关于android - 为什么 fragment 的父级在暂停后为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73062923/

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