Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackOverflowError when trying to inflate a custom layout for an AlertDialog inside a DialogFragment

Tags:

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:

@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 LogCat output:

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 can't get a LayoutInflator inside of onCreateDialog without causing infinite recursion, how do I inflate a view for a custom AlertDialog?

like image 540
Nora Powers Avatar asked Mar 01 '13 06:03

Nora Powers


People also ask

What is the difference between dialog & DialogFragment?

DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs. It allows the FragmentManager to manage the state of the dialog and automatically restore the dialog when a configuration change occurs.

How do I start a DialogFragment?

Step 1: Create an Android Project with empty Activity. Step 2: Create a Fragment and Open your java and XML file add these codes. Step 3: Add this code to your Fragment XML file Source Code. Step 4: Add these code In your activity button click whare open DialogFragment.


1 Answers

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/2020340

I'm not exactly sure if this is good form, because it doesn't really seem like it, but I replaced

getLayoutInflater(savedInstanceState) 

with

getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

Edit: They are the same. See this answer for details: https://stackoverflow.com/a/20995083/2020340

like image 169
Nora Powers Avatar answered Sep 24 '22 19:09

Nora Powers