Denys
Denys

Reputation: 4557

Error inflating class fragment from the xml

I found few similar questions here, but the proposed answers do not suit me.

So the error is :

08-13 14:40:10.723: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragments/com.example.fragments.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

My MainActivity is:

    package com.example.fragments;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

My xml is :

    <?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="horizontal" >

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="150dip"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        class="com.example.fragments.ListFragment" >
    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.fragments.DetailFragment" >
    </fragment>

</LinearLayout>

Would be very grateful for any help regards this issue

xx

Upvotes: 1

Views: 6654

Answers (2)

Denys
Denys

Reputation: 4557

So basically the problem was solved by implementing all the Fragment - related imports within the project as follows:

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment

Rather than:

import android.app.Fragment
import android.app.FragmentActivity
import android.app.ListFragment

Upvotes: 4

Iain_b
Iain_b

Reputation: 1053

I think the error might result from

android:layout_marginTop="?android:attr/actionBarSize"

actionBarSize is only introduced as of api 11 and as you're using the support library I assume you're developing on a pre Android 3.0 device. Try removing that property and see if it is the problem.

Upvotes: 1

Related Questions