尝试为DialogFragment内的AlertDialog充气自定义布局时出现StackOverflowError

时间:2013-03-01 06:21:59

标签: java android android-layout android-dialogfragment

我正在尝试使用Builder并设置自定义视图来创建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提供视图?

3 个答案:

答案 0 :(得分:26)

如果发现问题。 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

答案 1 :(得分:1)

在片段1.2.3:https://developer.android.com/jetpack/androidx/releases/fragment#1.2.3中,此问题似乎已解决。

文档确实使用requireActivity().getLayoutInflater()而不只是使用getLayoutInflater,因此仍不建议使用。

答案 2 :(得分:0)

您应该使用LayoutInflater.from(getContext())

如果您使用的是viewbindings,可以这样称呼它:

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