3

I'm having this problem:

android.view.InflateException: Binary XML file line #31: Error inflating class fragment Fragment com.myapp.mgm.QueryFragment did not create a view.

This problem is only happening when in tablet mode. If app runs on phone, it works ok.

I have this layout-large xml file (fragments.xml):

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/lightgrey">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/lightgrey"
              android:baselineAligned="false">

    <fragment
            android:name="com.myapp.mgm.QueryFragment"
            android:id="@+id/fragment1"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            tools:layout="@layout/nu"/>

    <fragment
            android:name="com.myapp.mgm.Infodata"
            android:id="@+id/fragment2"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            tools:layout="@layout/maindata"/>

</LinearLayout>
        <ListView
                android:id="@+id/list_slidermenu"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:choiceMode="singleChoice"
                android:divider="@color/orange"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/menulist_selector"
                android:background="@color/lblack"/>
</android.support.v4.widget.DrawerLayout>

As you can see I have two fragments and one Navigation drawer on my layout.

The class that puts all of this together is this:

public class Fragments extends ActionBarActivity implements QueryFragment.OnQuerySelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragments);

        slidingMenu();

        if (findViewById(R.id.fragmentsboth) != null) {
            if (savedInstanceState != null) {
                return;
            }
            QueryFragment firstFragment = new QueryFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentsboth, firstFragment).commit();
        }
    }
}

And my QueryFragment class is this:

public class QueryFragment extends ListFragment {
    public interface OnQuerySelectedListener {
        public void onItemSelected(Bundle itemSelectedData);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        operation = Fragments.operation;
        if (operation == 0) {
            layoutToShow = inflater.inflate(R.layout.querylist, container, false);
            ...
        } else if (operation == 1) {
            layoutToShow = inflater.inflate(R.layout.querylist, container, false);
            ...
        } else if (operation == 2) {
            layoutToShow = inflater.inflate(R.layout.search, container, false);
            ...
        } else if (operation == 3) {
            layoutToShow = inflater.inflate(R.layout.nu, container, false);
            ...
        }
        return layoutToShow;
    }

}

And my InfoData class as this stuff:

public class Infodata extends Fragment implements OnSeekBarChangeListener, OnItemSelectedListener {

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

        View itemDataLayout = inflater.inflate(R.layout.maindata, container, false);
        ....

        if (savedInstanceState != null && savedInstanceState.size()>=2) {
            layoutType = savedInstanceState.getInt("layout");
            comesFromSavedState = true;
            itemData = savedInstanceState;
        }
        return itemDataLayout;
    }

    @Override
    public void onStart() {
        super.onStart();

        Bundle args = getArguments();
        if (args != null) {
            populateLayoutWithData(1, args);
        } else if(comesFromSavedState){
            populateLayoutWithData(2, itemData);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //Saves stuff (data) in a bundle to restore after screen rotation
    }   
}

Basically I have a listview on the left fragment (QueryFragment) and after the user selects one of the items (a cursor with data), the right fragment (InfoData) is populated with the extracted data of this cursor. If I select an intem from the left fragment and it's data its populated on the right fragment, since i'm saving data on onSaveInstanceState, if I rotate the screen, everything is working OK, that is, data is reloaded on the right fragment.

Now, my Navigadion Drawer as 4 options. Each one of this options, loads a listView on the left fragment, and then, as before, if the user selects one of those items, it's data is loaded on the right fragment.

Here is the code that handles this in my Fragments class:

private void selectItem(int position) {
    int typeOfLayout;
    mDrawerList.setItemChecked(position, true);
    mDrawerLayout.closeDrawer(mDrawerList);
    QueryFragment newFragment;
    FragmentTransaction transaction;

    ViewGroup parentView = (ViewGroup) findViewById(R.id.fragmentsboth);

    if (parentView != null) {
        typeOfLayout = R.id.fragmentsboth;
    } else {
        typeOfLayout = R.id.fragment1;
    }

    switch (position) {
        case 0:
            Intent i = new Intent(this, NutriData.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            finish();
            break;
        case 1:
            newFragment = new QueryFragment();
            operation = 0;
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(typeOfLayout, newFragment).commit();
            break;
        case 2:
            newFragment = new QueryFragment();
            operation = 1;
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(typeOfLayout, newFragment).commit();
            break;
        case 3:
            newFragment = new QueryFragment();
            operation = 2;
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(typeOfLayout, newFragment).commit();
            break;
        case 4:
            newFragment = new QueryFragment();
            operation = 3;
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(typeOfLayout, newFragment).commit();
            break;
    }
}

Imagine this senario:

I got a listViewwith item's on the left fragment and then select one of these items. Then, it's data is passed to the right fragment that put's it on the right place. If I rotate the screen, data is saved and after rotation is reloaded on the right fragment. Now, I open the navigation drawer and select one of the options (1,2,3 or 4) and it's corresponding listView is loaded on the left fragment. The right fragment keeps showing the data from the first item the user as selected. Now, I press on a new item on the left fragment and it's data is loaded on the right fragment. Sweet. It's working as I wanted to. The problem is if I now rotate my screen. I'm getting this error:

01-01 15:51:16.356    2569-2569/com.myapp.mgm E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.mgm/com.myapp.mgm.Fragments}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
            at android.app.ActivityThread.access$700(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
            at android.app.Activity.setContentView(Activity.java:1895)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.myapp.mgm.Fragments.onCreate(Fragments.java:27)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
            at android.app.ActivityThread.access$700(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Fragment com.myapp.mgm.QueryFragment did not create a view.
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:308)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
            at android.app.Activity.setContentView(Activity.java:1895)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.myapp.mgm.Fragments.onCreate(Fragments.java:27)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
            at android.app.ActivityThread.access$700(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Can someone please explain me what I'm doing wrong?

Favolas
  • 6,963
  • 29
  • 75
  • 127

3 Answers3

0

Try removing

xmlns:android="http://schemas.android.com/apk/res/android

from LinearLayout.

0

You could make an attempt to put a layout as the container of the fragment that will be replaced by new one. Instead of embedding QueryFragment in fragments.xml directly.

1) fragments.xml:

<!-- change fragment1 to a layout -->
<FrameLayout
    android:id="@+id/fragment1_container"
    .../>

2) Fragments onCreate():

setContentView(R.layout.fragments);

// add QueryFragment to the container layout
getSupportFragmentManager()
    .beginTransaction()
    .add(R.id.fragment1_container, new QueryFragment())
    .commit();

3) Fragments selectItem():

if (parentView != null) {
    typeOfLayout = R.id.fragmentsboth;
} else {
    // change to the container layout id
    typeOfLayout = R.id.fragment1_container;
}

Error inflating class fragment | fragment did not create a view

Community
  • 1
  • 1
kuokuo
  • 196
  • 1
  • 4
0

A challenge commonly faced during in Android is the potentially frequent destruction and reconstruction of an Activity. The most common time this occurs is when the user rotates the device between landscape and portrait orientations. This crashing usually occurs because device orientations changes cause the Android framework to tear down the displayed Activity along within any contained Views, and then to fully reconstruct the Activity/View hierarchy. Any references to the Activity or to the Views suddenly become invalid.

NOTE: This problem can be solved by including:-

android:configChanges="orientation" android attribute in all your activities in the android manifest. This prevents the tear-down/reconstruction process caused by orientation changes

Additionally, in your Fragment's constructor dont forget to do this

public MyFragment()
    {
        setRetainInstance(true);
    }

Calling setRetainInstance() with a value of true causes Android to preserve the Fragment across the tear-down/reconstruction cycle of an activity. Along with the Views or other Object references contained within the Fragment or Views remain.

With this set to true, Android :-

  1. Holds a reference to the Fragment instance
  2. Tears down the old activity instance
  3. Creates a new Activity instance
  4. Attaches the preserved Fragment instance to the new activity instance

This saved me alot after trying out all these mad solutions, this was the only one which worked.

Otieno Rowland
  • 2,182
  • 1
  • 26
  • 34
  • Even if one decides to ultimately do this change for other reasons, it is only pushing the problem deeper, so that crashes will happen less often. That is, there are other situations where Android may decide to tear down your app, and you have not fixed the underlying problem. For example, suppose user receives a phone call while running your app? They answer the call, so your app is no longer in foreground. Then perhaps they do other actions on device. By the time they return to your app, Android might need to recreate everything. Oops - your lurking bug happens. Now how do you debug? – ToolmakerSteve Nov 11 '15 at 15:10
  • CLARIFICATION: I'm commenting on `android:configChanges="orientation"`, which suppresses tear-down on rotations. While this may be useful if you do not want to switch to a different layout via Android's layout mechanism, using it to suppress a crash is just avoiding whatever is actually wrong in your code. It will bite you eventually - find and fix the problem first. Try `setRetainInstance` by itself first, and make sure the exception does not occur when you rotate. If that works, then consider whether to suppress orientation change. – ToolmakerSteve Nov 11 '15 at 15:19