Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment inflated in Fragment Dialog throws error "Fragment did not create a view"

The user clicks a button which brings up a fragment dialog that inflates a fragment like this:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        View view = getActivity().getLayoutInflater().inflate(
                R.layout.monday_fragment, null);
        builder.setView(view).setTitle("Homework Due Monday")
                .setNegativeButton("Dismiss", null);

        AlertDialog dialog = builder.create();
        dialog.show();

Here is the xml of the layout specified (monday_fragment.xml) where a reference to the fragment to be inflated lies:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <fragment
        android:id="@+id/monday_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

The fragment in the fragment dialog then inflates its own layout in its OnCreateView:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        swipe = inflater.inflate(R.layout.day_homework_fragment, container,
                false);

        lView = (ListView) swipe.findViewById(R.id.listView1);

        progress = (ProgressBar) swipe.findViewById(R.id.progress);

        lView.setVisibility(View.GONE);

        return swipe;

    }

Here is the "day_homework_fragment.xml" layout that is inflated in the fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:background="@color/light_background">

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
         android:background="#ffffff"
         android:divider="@null"
        android:dividerHeight="0dp"
         >
    </ListView>

           <TextView 
           android:id="@+id/emptyView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="As of now, no homework due this day"
           android:padding="30dp" 
           android:textSize="16sp"/>

</LinearLayout>

Here is the log of the error it throws, I've tried both this post and this post and neither have fixed the issue.

  10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime: FATAL EXCEPTION: main
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime: Process: com.bernard.beaconportal.activities, PID: 786
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:668)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(<Xposed>)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.bernard.beaconportal.activities.schedule.view.MondayView.showDialog(MondayView.java:232)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.bernard.beaconportal.activities.schedule.view.MondayView$1.onClick(MondayView.java:174)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.View.performClick(View.java:4780)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19867)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5338)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: Fragment com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment did not create a view.
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2273)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:278)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.BaseFragmentActivityDonut.onCreateView(BaseFragmentActivityDonut.java:44)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:78)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:842)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:830)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:668) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(<Xposed>) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.bernard.beaconportal.activities.schedule.view.MondayView.showDialog(MondayView.java:232) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.bernard.beaconportal.activities.schedule.view.MondayView$1.onClick(MondayView.java:174) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.View.performClick(View.java:4780) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19867) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5338) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115) 

Edit:I just realized that this only started happening after I moved from eclipse to Android Studio. Maybe that has something to do with it? Possibly all the fragment references are messed up?

like image 328
I'm_With_Stupid Avatar asked Oct 11 '15 06:10

I'm_With_Stupid


2 Answers

You cannot put fragment inside a standard AlertDialog. You have to create a DialogFragment implementation for adding fragment to your dialog. The reason is very simple, as a standard fragment will try to attach itself to the root window of the corresponding activity which is not the case when AlertDialog has a separate window over the root window. AlertDialog cannot contain a DialogFragment or even a Fragment. Try changing your AlertDialog code to DialogFragment implementation.

like image 178
JavaGhost Avatar answered Oct 18 '22 16:10

JavaGhost


The fact that your log points to line 10 means that the xml declaration here is an issue as fragment says on line 10:

 android:name="com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment"

I would double check this reference here to see if it points to the correct place that you have saved the java class for the Fragment.

Also, check in your MondayFragment class that you are extending the correct Fragment class. In other words, if you are using the support library, you need to extend the support library Fragment class and not the normal Fragment class.

If all else fails, you can always programmatically add on a fragment: http://developer.android.com/training/basics/fragments/fragment-ui.html

EDIT: I see that you are currently inflating your xml using the following code:

View view = getActivity().getLayoutInflater().inflate(
                R.layout.monday_fragment, null);

Since you are inflating it inside a fragment anyway, can you not just say:

View view = inflater.inflate(R.layout.monday_fragment, null,
                false);
like image 29
Simon Avatar answered Oct 18 '22 16:10

Simon