StackOverflowError when trying to inflate custom layout for AlertDialog inside DialogFragment dialog box

I am trying to create an AlertDialog using Builder and customizing a custom view. When I try to inflate the view inside onCreateDialog, I get a StackOverflowError ..

Here is the code until it returns to 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); ... } 

And here is the output of 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 

EDIT: I found this line in the source of DialogFragment.getLayoutInflater () :

 mDialog = onCreateDialog(savedInstanceState); 

So, if I cannot get a LayoutInflator inside onCreateDialog without causing infinite recursion, how can I inflate a view for a custom AlertDialog?

+7
source share
1 answer

If a problem is detected. DialogFragment.getLayoutInflater () contains a call to onCreateDialog() , so calling onCreateDialog() from getLayoutInflater() creates an infinite loop.

I found a solution in this answer: stack overflow

I’m not quite sure that this is a good form, because in fact it does not look like it, but I replaced

 getLayoutInflater(savedInstanceState) 

from

 getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
+13
source

All Articles