Psest328
Psest328

Reputation: 6675

Fragments Failed to inflate

I'm a little shakey on this so bear with me. I'm trying to teach myself and I'm currently working out of the WROX Android 4 Application Development for Beginners book. I'm currently on the section "Communication between fragments" and I feel like I'm getting it, but when I run the code taken right out of the book, I get "failed to inflate" in the logcat. Here's the XMLs followed by the .java files, then the error. I can't figure out what's killing this:

main.xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >
    <fragment
        android:name="com.example.Fragments.Fragment1"
        android:id="@+id/fragment1"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent"
        />
    <fragment
        android:name="com.example.Fragments.Fragment2"
        android:id="@+id/fragment2"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent"
        />
</LinearLayout>

fragment1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00FF00"
    >
    <TextView 
        android:id="@+id/lblFragment1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #1"
        android:textColor="#000000"
        android:textSize="25sp"
        />
</LinearLayout>

fragment2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFE00"
    >
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #2"
        android:textColor="#000000"
        android:textSize="25sp"
        />
    <Button
        android:id="@+id/btnGetText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get text in Fragment #1"
        android:textColor="#000000"
        android:onClick="onClick"
        />
</LinearLayout>

FragmentsActivity.java:

package com.example.fragments;

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

public class FragmentsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Fragment1.java:

package com.example.fragments;

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

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

}

Fragment2.java:

package com.example.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

    @Override
    public void onStart() {
        super.onStart();
        //---Button View---
        Button btnGetText = (Button)
                getActivity().findViewById(R.id.btnGetText);
        btnGetText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                TextView lbl = (TextView)
                        getActivity().findViewById(R.id.lblFragment1);
                Toast.makeText(getActivity(), lbl.getText(), 
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

logcat:

04-20 15:09:55.759: E/ActivityThread(9523): Failed to inflate
04-20 15:09:55.759: E/ActivityThread(9523): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:282)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.setContentView(Activity.java:1881)
04-20 15:09:55.759: E/ActivityThread(9523):     at com.example.fragments.FragmentsActivity.onCreate(FragmentsActivity.java:15)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.performCreate(Activity.java:5104)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.os.Looper.loop(Looper.java:137)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.main(ActivityThread.java:5195)
04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.reflect.Method.invoke(Method.java:511)
04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-20 15:09:55.759: E/ActivityThread(9523):     at dalvik.system.NativeStart.main(Native Method)
04-20 15:09:55.759: E/ActivityThread(9523): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.Fragments.Fragment1: make sure class name exists, is public, and has an empty constructor that is public
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:592)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:560)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.onCreateView(Activity.java:4709)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-20 15:09:55.759: E/ActivityThread(9523):     ... 21 more
04-20 15:09:55.759: E/ActivityThread(9523): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.Fragments.Fragment1" on path: /data/app/com.example.fragments-2.apk
04-20 15:09:55.759: E/ActivityThread(9523):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:582)
04-20 15:09:55.759: E/ActivityThread(9523):     ... 24 more

Upvotes: 3

Views: 2666

Answers (2)

Blackbelt
Blackbelt

Reputation: 157437

the Layout definition is wrong android:name="com.example.Fragments.Fragment1". It should be

android:name="com.example.fragments.Fragment1"

the same for Fragment2

Upvotes: 4

quietmint
quietmint

Reputation: 14153

It looks like you mistyped the package name.

Logcat shows that it can't find the class com.example.Fragments.Fragment1. Your source code for Fragment1 shows that you named the package com.example.fragments instead of com.example.Fragments. Capitalize the package name, or lowercase the references to it.

Upvotes: 1

Related Questions