Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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.

like image 497
Killerpixler Avatar asked Mar 17 '26 12:03

Killerpixler


1 Answers

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>
like image 60
Zsombor Erdődy-Nagy Avatar answered Mar 19 '26 01:03

Zsombor Erdődy-Nagy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!