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

后端 未结 3 1333
野趣味
野趣味 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: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

提交回复
热议问题