Dificultades con el diseño básico de fragmentos

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:

  • FragmentsActivity.java
  • ExamplesFragment.java
  • ExamplesFragment2.java
  • examples_fragment.xml
  • examples_fragment2.xml
  • main.xml

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.

preguntado el 08 de noviembre de 11 a las 16:11

Did you fix this? I noticed you're importing the wrong Fragment. En lugar de android.support.v4.app.Fragment Deberías usar android.app.Fragment. -

Correct, you should import android.app.Fragment; -

3 Respuestas

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>

respondido 08 nov., 11:20

i still get the same error: 11-07 18:39:35.524: E/AndroidRuntime(790): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mwerner.fragments/com.mwerner.fragments.FragmentsActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment - Killerpixler

can you help me? I tried your xml file but still get the above mentioned error (crash on startup). Any help would be very much appreciated - Killerpixler

Had the same problem. My issue was also the class attribute - my namespace I defined in my Layout was incorrect (did not match the namespace in my Fragment class). - Cenizas a las cenizas

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);
        }
    }

Respondido el 13 de diciembre de 11 a las 21:12

Not sure which version that comment is against, but when I do this, I still get the inflate exception. You should keep the Activity to extend Dialog. - Cenizas a las cenizas

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

Respondido 10 Jul 12, 15:07

Not sure which version that comment is against, but when I do this, I still get the inflate exception. You should keep the Activity to extend Dialog. - Cenizas a las cenizas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.