Killerpixler
Killerpixler

Reputation: 4050

Difficulties with basic fragment layout

I'm trying to get my head around fragments in order to have my apps prepped for ICS.

I have the following files to just get the most basic fragment app you can have. It should have this when launched: One Fragment Layout with a text view "Fragment 1" and next to it another Fragment Layout with "Fragment2".

My package name is com.mwerner.fragments

My files are:

The code for FragmentsActivity.java is:

package com.mwerner.fragments;

import android.app.Activity;
import android.os.Bundle;

public class FragmentsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

ExamplesFragment.java

package com.mwerner.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExamplesFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.examples_fragment, container, false);
    }
}

ExamplesFragment2.java

package com.mwerner.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExamplesFragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.examples_fragment2, container, false);
    }
}

The examples_fragment.xml files just have a linear layout with a textview in it... Here is the code for the main.xml:

<?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" >
<fragment
        class="com.mwerner.fragments$ExamplesFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
         />

<fragment 
        class="com.mwerner.fragments$ExamplesFragment2"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent" />


</LinearLayout>

The app crashes on startup with the error

11-07 18:12:12.519: E/AndroidRuntime(696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mwerner.fragments/com.mwerner.fragments.FragmentsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

Can you please tell me what is wrong here? I pretty much copied / pasted the code from the google developers page for fragments.

Upvotes: 3

Views: 3602

Answers (3)

karn
karn

Reputation: 6033

When I was writing my first program on Fragments using I incurred same error. Instead of extending "Activity" extend "FragmentActivity" in your launcher activity.

Upvotes: 0

MGK
MGK

Reputation: 7098

Try extending FragmentActivity

  public class FragmentsActivity extends FragmentActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }

Upvotes: 0

Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

You defined the path to your fragments incorrectly in your layout xml. Correct the class attributes. Try this:

<?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" >
<fragment
        class="com.mwerner.fragments.ExamplesFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
         />

<fragment 
        class="com.mwerner.fragments.ExamplesFragment2"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent" />


</LinearLayout>

Upvotes: 5

Related Questions