StackOverflowError when trying to inflate a custom layout for an AlertDialog inside a DialogFragment

后端 未结 3 1287
野趣味
野趣味 2021-02-12 13:50

I\'m trying to create an AlertDialog, by using the Builder and setting a custom view. When I try to inflate the view inside of onCreateDialog, I get a StackOverflowError..

相关标签:
3条回答
  • 2021-02-12 14:07

    It looks like this is fixed in Fragment 1.2.3: https://developer.android.com/jetpack/androidx/releases/fragment#1.2.3.

    The docs do use requireActivity().getLayoutInflater() rather than just getLayoutInflater however so it still might not be recommended.

    0 讨论(0)
  • 2021-02-12 14:11

    If found the problem. DialogFragment.getLayoutInflater() contains a call to onCreateDialog(), so calling onCreateDialog() from within getLayoutInflater() creates an infinite loop.

    I found the solution in this answer: https://stackoverflow.com/a/10585164/2020340

    I'm not exactly sure if this is good form, because it doesn't really seem like it, but I replaced

    getLayoutInflater(savedInstanceState)
    

    with

    getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

    Edit: They are the same. See this answer for details: https://stackoverflow.com/a/20995083/2020340

    0 讨论(0)
  • 2021-02-12 14:16

    You should use LayoutInflater.from(getContext()).

    If you are using viewbindings you can call it like this:

    binding = FragmentOtpDialogBinding.inflate(LayoutInflater.from(getContext()));

    0 讨论(0)
提交回复
热议问题