How to inflate LinearLayout in a fragment?

user1298799 picture user1298799 · Jun 26, 2012 · Viewed 14.9k times · Source

I want to inflate a LinearLayout into a fragment in the "onCreateView" method. But I have always the same error:

"android.view.InflateException: Binary XML file line #7: Error inflating class fragment"

I am using the android.support.v4.app.FragmentActivity in a "libs" folder.

Main activity:

public class GeofranceDeptActivity extends FragmentActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dept);

    }
}

The main xml file dept:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment class="dauran.geofrance.DeptFragment"
            android:id="@+id/depts" android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent"> 
            </fragment>

    <FrameLayout android:id="@+id/detailsdept" android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent" />

</LinearLayout>

The class for the fragment DeptFragment:

public class DeptFragment extends Fragment implements OnTouchListener{

    Context mContext = getActivity();

    boolean mDualPane;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
              return null;
            }

    return (LinearLayout)inflater.inflate(R.layout.dept_fragment, container, false);
        }


    @Override
    public void onActivityCreated(Bundle savedState) {
        ................etc

The xml file dept_fragment that I want to inflate :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >
    <ImageView
        android:id="@+id/targetDeptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/region_france_grand" />

    <ImageView
        android:id="@+id/cacheDeptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/region_france_grand" />
    <Spinner
        android:id="@+id/sDepartement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/targetImage" />

</RelativeLayout>
</LinearLayout>

The Logcat:

06-26 19:02:27.940: E/AndroidRuntime(11993): FATAL EXCEPTION: main
06-26 19:02:27.940: E/AndroidRuntime(11993): java.lang.RuntimeException: Unable to start activity ComponentInfo{dauran.geofrance/dauran.geofrance.GeofranceDeptActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.os.Looper.loop(Looper.java:137)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at java.lang.reflect.Method.invokeNative(Native Method)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at java.lang.reflect.Method.invoke(Method.java:511)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at dalvik.system.NativeStart.main(Native Method)
06-26 19:02:27.940: E/AndroidRuntime(11993): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.Activity.setContentView(Activity.java:1835)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at dauran.geofrance.GeofranceDeptActivity.onCreate(GeofranceDeptActivity.java:10)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.Activity.performCreate(Activity.java:4465)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-26 19:02:27.940: E/AndroidRuntime(11993):    ... 11 more
06-26 19:02:27.940: E/AndroidRuntime(11993): Caused by: java.lang.IllegalStateException: Fragment dauran.geofrance.DeptFragment did not create a view.
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:293)
06-26 19:02:27.940: E/AndroidRuntime(11993):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)

Answer

user picture user · Jun 26, 2012

The Logcat probably says more then android.view.InflateException: Binary XML file line #7: Error inflating class fragment(check below to see if you don't have an instantiation exception, maybe you could add the full exception?). After looking at your code I would assume that your DeptFragment isn't situated in the dauran.geofrance package and because of this android can't find it.

Also, there is no need to test for null the container parameter:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dept_fragment, container, false);
}

Also, I don't see the need for wrapping the RelativeLayout with a LinearLayout in the R.layout.deptfragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
    <ImageView
        android:id="@+id/targetDeptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/region_france_grand" />

    <ImageView
        android:id="@+id/cacheDeptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/region_france_grand" />
    <Spinner
        android:id="@+id/sDepartement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/targetImage" />

</RelativeLayout>