StackOverflow尝试在 DialogFragment 中为 AlertDialog 扩充自定义布局时出错

我正在尝试通过使用生成器并设置自定义视图来创建警报对话框。当我尝试在 onCreateDialog 内部放大视图时,我得到一个 StackOverflowError。

以下是代码,直到它循环回 onCreateDialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.enter_time);

    LinearLayout outerLayout = (LinearLayout) getLayoutInflater(savedInstanceState)
            .inflate(R.layout.time_entry_dialog, null);
    ...
}

下面是 LogCat 输出:

02-28 22:30:04.220: E/AndroidRuntime(4250): FATAL EXCEPTION: main
02-28 22:30:04.220: E/AndroidRuntime(4250): java.lang.StackOverflowError
02-28 22:30:04.220: E/AndroidRuntime(4250):     at android.app.Activity.getSystemService(Activity.java:4009)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at android.view.LayoutInflater.from(LayoutInflater.java:210)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:75)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:812)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:374)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at com.sweatyreptile.chee.runtimetracker.TimeEntryDialogFragment.onCreateDialog(TimeEntryDialogFragment.java:18)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at com.sweatyreptile.chee.runtimetracker.TimeEntryDialogFragment.onCreateDialog(TimeEntryDialogFragment.java:21)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at com.sweatyreptile.chee.runtimetracker.TimeEntryDialogFragment.onCreateDialog(TimeEntryDialogFragment.java:21)
02-28 22:30:04.220: E/AndroidRuntime(4250):     at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
...etc

编辑:我在DialogFragment.getLayoutInflater()的源代码中找到了这行:

mDialog = onCreateDialog(savedInstanceState);

所以。。如果我无法在 onCreateDialog 中获取 LayoutInflator 而不导致无限递归,那么如何为自定义 AlertDialog 扩充视图?


答案 1

如果发现问题。DialogFragment.getLayoutInflater() 包含对 的调用,因此从内部调用会创建一个无限循环。onCreateDialog()onCreateDialog()getLayoutInflater()

我在这个答案中找到了解决方案:https://stackoverflow.com/a/10585164/2020340

我不完全确定这是否是好的形式,因为它看起来并不像,但我替换了

getLayoutInflater(savedInstanceState)

getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

编辑:它们是相同的。有关详细信息,请参阅此答案:https://stackoverflow.com/a/20995083/2020340


答案 2

看起来这在片段 1.2.3 中已修复:https://developer.android.com/jetpack/androidx/releases/fragment#1.2.3

这些文档确实使用,而不仅仅是使用,因此可能仍然不建议使用。requireActivity().getLayoutInflater()getLayoutInflater


推荐