Android: Fragment v4 support exception

After trying out fragments without using the v4 support library, I tried a layout using fragments and kept running into this exception.


07-18 05:57:51.821: E/AndroidRuntime(2082): FATAL EXCEPTION: main
07-18 05:57:51.821: E/AndroidRuntime(2082): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mfcoding.locationBP/com.mfcoding.locationBP.UI.HelloFragmentActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.os.Looper.loop(Looper.java:137)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at java.lang.reflect.Method.invoke(Method.java:511)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at dalvik.system.NativeStart.main(Native Method)
07-18 05:57:51.821: E/AndroidRuntime(2082): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.Activity.setContentView(Activity.java:1835)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at com.mfcoding.locationBP.UI.HelloFragmentActivity.onCreate(HelloFragmentActivity.java:14)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.Activity.performCreate(Activity.java:4465)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	... 11 more
07-18 05:57:51.821: E/AndroidRuntime(2082): Caused by: java.lang.ClassCastException: com.mfcoding.locationBP.UI.fragments.HelloFragment cannot be cast to android.app.Fragment
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.Fragment.instantiate(Fragment.java:574)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.Fragment.instantiate(Fragment.java:549)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.app.Activity.onCreateView(Activity.java:4235)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
07-18 05:57:51.821: E/AndroidRuntime(2082): 	... 21 more

What does this exception mean and how do I fix it? It’s pretty simple. This is the main exception
com.mfcoding.locationBP.UI.fragments.HelloFragment cannot be cast to android.app.Fragment
I had made sure my fragments were using v4 support, but what I wasn’t doing was having my Activity extends FragmentActivity instead of Activity.
So your HelloFragmentActivity needs to be

class HelloFragmentActivity extends FragmentActivity {

}

extending FragmentActivity makes sure that it can using v4 fragments.

Pretty simple huh. Hope you didn’t spend as much time trying to figure it out as I did. Took me over an hour!!!!

Advertisements
This entry was posted in Android, troubleshooting and tagged , , , , . Bookmark the permalink.

3 Responses to Android: Fragment v4 support exception

  1. erasmosoares says:

    This is not working to me, I got the same erro.:
    ####

    package com.erasmo.mapsv2;

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;

    public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    }

    Am I doing something wrong?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s