我不确定这是否是一种很好的形式,因为它看起来并不是真的,但是我替换了getLayoutInflater(savedInstanceState)使用getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 编辑:它们是相同的.有关详细信息,请参见以下答案: https://stackoverflow.com/a/20995083/2020340 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..Here is the code up to the point where it loops back to onCreateDialog:@Overridepublic 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); ...}And here is the LogCat output:02-28 22:30:04.220: E/AndroidRuntime(4250): FATAL EXCEPTION: main02-28 22:30:04.220: E/AndroidRuntime(4250): java.lang.StackOverflowError02-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)...etcEDIT: I found this line in the source of DialogFragment.getLayoutInflater():mDialog = onCreateDialog(savedInstanceState);So.. if I can't get a LayoutInflator inside of onCreateDialog without causing infinite recursion, how do I inflate a view for a custom AlertDialog? 解决方案 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/2020340I'm not exactly sure if this is good form, because it doesn't really seem like it, but I replacedgetLayoutInflater(savedInstanceState)withgetActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);Edit: They are the same. See this answer for details: https://stackoverflow.com/a/20995083/2020340 这篇关于尝试为DialogFragment内部的AlertDialog增加自定义布局时的StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-07 13:06