Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error inflating fragment from layout android-support-v4

I'm having troubles working with fragments to the 2.2 platform. I imported the android-support-v4.jar library to support fragments.
I tried to change the target to Honeycomb 3.0, and the code works. So, I wanna know if there exists a way to work with fragments on a native platform and a different way to do that with prior versions.

Here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nsy.testefragmentos"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".TesteFragmentosActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity android:name="FragmentoLista" />
            <activity android:name="FragmentoDetalhe" />
    </application>
</manifest>

And here is my main class

package com.nsy.testefragmentos;

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

public class TesteFragmentosActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

And finally, the fragment called from the main.xml file:

package com.nsy.testefragmentos;

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

public class FragmentoLista extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);         
    }   
}

when I run, the follow error appears in the logCat:

12-24 11:46:16.708: E/AndroidRuntime(395): FATAL EXCEPTION: main
12-24 11:46:16.708: E/AndroidRuntime(395): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nsy.testefragmentos/com.nsy.testefragmentos.TesteFragmentosActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.os.Looper.loop(Looper.java:123)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.reflect.Method.invokeNative(Native Method)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.reflect.Method.invoke(Method.java:521)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-24 11:46:16.708: E/AndroidRuntime(395):  at dalvik.system.NativeStart.main(Native Method)
12-24 11:46:16.708: E/AndroidRuntime(395): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.Activity.setContentView(Activity.java:1647)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.nsy.testefragmentos.TesteFragmentosActivity.onCreate(TesteFragmentosActivity.java:12)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-24 11:46:16.708: E/AndroidRuntime(395):  ... 11 more
12-24 11:46:16.708: E/AndroidRuntime(395): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.nsy.testefragmentos-1.apk]
12-24 11:46:16.708: E/AndroidRuntime(395):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
12-24 11:46:16.708: E/AndroidRuntime(395):  ... 20 more
12-24 11:46:21.828: I/Process(395): Sending signal. PID: 395 SIG: 9

I really don't know what I'm doing wrong =/ Can anyone help me? thnkx

like image 807
Natan Lotério Avatar asked Dec 24 '11 14:12

Natan Lotério


1 Answers

Your activity should extend FragmentActivity instead of Activity. Had the same problem myself.

public class TesteFragmentosActivity extends FragmentActivity {
    ...
}
like image 64
Boude Avatar answered Sep 22 '22 03:09

Boude