StackOverflow尝试在 DialogFragment 中为 AlertDialog 扩充自定义布局时出错
2022-09-02 03:26:22
我正在尝试通过使用生成器并设置自定义视图来创建警报对话框。当我尝试在 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 扩充视图?