android-fragments 为什么暂停后片段的父级为空?

cotxawn7  于 2022-11-14  发布在  Android
关注(0)|答案(3)|浏览(211)

当我运行应用程序和使用它没有崩溃。BottomSheetBehavior工作正常。但如果我返回应用程序后,在后台暂停了很长一段时间,它崩溃与空点异常时,试图铸造view.parent as View,以找到与BottomSheetBehavior的视图。
为什么片段在暂停后没有父项?如何修复?
我试着做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:
第一个

kt06eoxx

kt06eoxx1#

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

方式1:

behavior放在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)
            }

        })
    }
pnwntuvh

pnwntuvh2#

首先在onCreateView()中扩展布局,然后可以在onViewCreated()中完成所有与视图相关的工作。

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    binding = FragmentToolboxSheetBinding.inflate(layoutInflater)
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    behavior = BottomSheetBehavior.from(view)
}
e3bfsja2

e3bfsja23#

请不要忽略savedInstanceState,并确保Fragment甚至连接到任何Context。抱歉,没有提供功能完整的答案;例如,onAttached()可以确定Fragment是附加的-并且savedInstanceState可能是也可能不是null。并且当view.parent为空时,View可能不存在,它可能是MainActivitybehavior,而不是ToolboxSheetFragment,这不是一回事。

相关问题