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

7

10

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?

Favian answered 11/10, 2015 at 6:3 Comment(11)
did you call onCreateView or onCreate for your fragment?Sturrock
show the markup to your day_homework_fragment.xml fragmentChinch
@Tauqir I did call oncreateview for my fragment, it inflates "day_homework_fragment"Molasses
@Chinch added the markup for the "day_homework_fragment.xml"Molasses
from where you are calling AlertDialogBossism
@penguin from a button that is located in a fragmentMolasses
where is the code for your mondayfragment?Lakenyalaker
So you have a layout called monday_fragment and a fragment called monday_fragment? can you post the xml code for the layout monday_fragment too?? Maybe try changing the name of the monday_fragment layout to monday_layout and see what happensLakenyalaker
If you remove the code in OnCreateView(), does the app still crash? Just for testing.Jonejonell
I did and it still crashedMolasses
Then we know the problem is not with day_homework_fragment.xml layout. Ashame, I did not know that earlier.Jonejonell
G
5

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.

Granduncle answered 20/10, 2015 at 19:28 Comment(1)
I think you have a point, hence I upvoted your answer. Your suggestion is probably best in my view. However the author developer probably does not want to change much code. In this case, that may be a mistake.Jonejonell
J
4

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);
Jataka answered 13/10, 2015 at 19:29 Comment(2)
I'm fairly sure that I have the correct package name since even the IDE recognizes that I'm pointing to the correct fragment. Also I am using the support library for my Fragment class.Molasses
Try "File | Invalidate Caches/Restart" on Android Studios.Jataka
J
3

I think this code is suspect:

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

The first parameter of inflate should be the xml filename instead of the fragment ID. Perhaps you want xml R.layout.day_homework_fragment instead.

I know these IDs are confusing. Only methods like findViewById() points to an UI ID.

Jonejonell answered 11/10, 2015 at 7:1 Comment(2)
Oh sorry I should have been more clear in my post. "monday_fragment.xml" is also the name of the layout that holds the reference to the fragment. I will make that more clear in my post.Molasses
To clarify monday_fragment does not refer to a fragment id but to an xml layoutMolasses
B
3

Do following two things:

  1. Check if com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment is correct package name (path)

  2. In your monday_fragment.xml rename android:id="@+id/monday_fragment" to android:id="@+id/monday_fragment1".

    See following code:

    <Fragment
       android:id="@+id/monday_fragment1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:name="com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment"
       android:layout_centerHorizontal="true"/>
    
Bossism answered 13/10, 2015 at 20:6 Comment(3)
I'm fairly sure that I have the correct package name since even the IDE recognizes that I'm pointing to the correct fragment. Also I just tried #2 and it seems that the ID isn't conflicting with anything as I get the same error. It's a strange error, I have no idea what else to try.Molasses
if your are using Android studio, then delete all sub folders under [app module]/build/ and recompile projectBossism
Still no change after deleting build folder and recompiling. Now that I think about it this only started happening after I moved from eclipse to Android Studio, maybe that has something to do with it.Molasses
J
2

I believe you have to debug the package com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment itself. The UI references are probably not valid anymore after you migrated to Android Studio. One easy way to debug is to remove code in OnCreateView().

If you import the package as a jar file, then that's a different case/problem.

Jonejonell answered 16/10, 2015 at 6:11 Comment(1)
@I'm_With_Stupid, So...if you remove the code in OnCreateView, does the app still crash?Jonejonell
W
1

Why don't you use dialog fragment instead of the Alertdialog ??Its important for a frgament to be called within activity scope .And dialog fragment will have all life cycle method and which wil facilitate creation of any subsequent fragments .

Woadwaxen answered 26/11, 2015 at 19:2 Comment(0)
H
0

Binding a fragment through xml often entails negative subtle consequences. Do this dynamically.

Change your monday_fragment.xml like:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    >

</FrameLayout>

and change this chunk of code:

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);

MondayFragment mondayFragment = new MondayFragment();
getFragmentManager().beginTransaction()
                    .replace(R.id.container, mondayFragment)
                    .commit();

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

Edit: if it fails try to change

getFragmentManager().beginTransaction()
        .replace(R.id.container, mondayFragment)
        .commit();

for

getParentFragment().getChildFragmentManager().beginTransaction()
            .replace(R.id.container, mondayFragment)
            .commit();

Edit let's try DialogFragment

Please create class DayHomeworkDialogFragment:

public class DayHomeworkDialogFragment extends DialogFragment {

    public DayHomeworkDialogFragment() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {


        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View swipe = inflater.inflate(R.layout.day_homework_fragment, null);

        lView = (ListView) swipe.findViewById(R.id.listView1);
        progress = (ProgressBar) swipe.findViewById(R.id.progress);
        lView.setVisibility(View.GONE);



        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
                .setView(swipe)
                .setTitle("Homework Due Monday")
                .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("Dismiss",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                dialog.dismiss();
                            }
                        }
                );

        return alertDialogBuilder.create();
    }
}

and instead of 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(); 

use this:

FragmentManager fragmentManager = getFragmentManager();
DayHomeworkDialogFragment dayHomeworkDialogFragment = new DayHomeworkDialogFragment();
dayHomeworkDialogFragment.show(fragmentManager, "my_day_homework_fragment");

if it fails, try to use getChildFragmentManager() instead of getFragmentManager()

Hapless answered 26/11, 2015 at 20:53 Comment(4)
No matter what I try, I get this error. It claims R.id.container doesn't exist. I even tried initiating the FrameLayout before referencing it using framelayout.getId(). Nothing seems to work.Molasses
even getChildFragmentManager() didn't helped?Hapless
So what I ended up doing was making "com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment" a DialogFragment instead of a Fragment. I just wish we could have figured out why it through that initial error.Molasses
@Favian I have some thoughts about the initial error, but I need some time to read source code and test some assumptions. When I'm done, I'll let you know the reason of the problemHapless

© 2022 - 2024 — McMap. All rights reserved.