Siddharth
Siddharth

Reputation: 9574

Using FragmentActivity throws InflateException

Common Fragment to be used in 2 Activity's

public class Ads extends Fragment {
    private View rootView ;
    private MoPubView adView ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.ads, container, true) ;
        adView = (MoPubView) rootView.findViewById(R.id.adView) ;
        adView.setAdUnitId(LogoQuizUtil.MOPUB_AD_UNIT);     
        adView.loadAd();
        return rootView;
    }
}

Fragment layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ad_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="bottom" >

    <com.mopub.mobileads.MoPubView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:gravity="bottom" />    
</LinearLayout>

Other Layout where I include this fragment

 <fragment
    android:id="@+id/ads"
    android:name="myPackage.Ads"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="bottom" />

I get a dont get a exception when I call setContentView (R.layout.ads) the first time, when the second activity calls the fragementactivity (Ads) I get a exception at setContentView (R.id.ads).

Exception I get is

08-07 09:38:30.359: W/System.err(975): java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.SecondActivity}: android.view.InflateException: Binary XML file line #263: Error inflating class fragment

Upvotes: 0

Views: 1175

Answers (1)

Barak
Barak

Reputation: 16393

Lets start at the beginning. An activity is not a fragment and a fragment is not an activity.

You need an activity to contain/control fragments. To do that you extend Activity (for Honeycomb+ development) or use the support library and extend FragmentActivity. In this class you would use setContentView to set the layout that will contain the fragment(s).

You then, from that class, call the fragment manager (getFragmentManager or getSupportFragmentManager depending on which class you have extended). The fragment manager is then used to create, attach and detach the fragments.

The fragment, not being an activity, is coded differently. It should have an onCreateView method that inflates the layout for the fragment and returns it to the fragment manager from the activity.

Then it is typical to use onActivityCreated for the rest of your code (or whatever else is needed).

Making the changes to use fragments properly may or may not solve all of your problems, as I see you are using some kind of custom widget in your layout and the issue could be there too.

Upvotes: 3

Related Questions