1

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

Denys
  • 4,287
  • 8
  • 50
  • 80
  • are the classes com.example.fragments.ListFragment , com.example.fragments.DetailFragment present in your app? – nandeesh Aug 13 '12 at 15:14
  • Dear nandeesh, yes they are present in the project folder and are in the package com.example.fragments – Denys Aug 13 '12 at 16:04

2 Answers2

4

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
Denys
  • 4,287
  • 8
  • 50
  • 80
  • I'm new to Android but aren't classes in the android.support.v4.app package just backports of newer Android features, so you can use new features on older versions of Android? If changing the imports works, it works. But I'm still curious to know why the android.app implementations are unable to be inflated. – spaaarky21 Dec 14 '12 at 21:46
  • Your question is 100% fair but honestly - I dont know :) One way worked for me, other didnt. Simple as that :) At that time i didnt have enough time to go into details. Sorry xx – Denys Jan 02 '13 at 10:14
  • The reason is that your activity extends android.support.v4.app.FragmentActivity. android.support.v4.app and android.app are separate class trees so Fragment in one may look like Fragment in another but they're separate objects. If you check their inheritance hierarchies they diverge after Object. Only use the support library if you need to and you'l avoid this issue. – majinnaibu Jul 04 '13 at 00:08
1

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.

Iain_b
  • 1,043
  • 5
  • 13