26

I am trying to inflate a layout containing a Fragment using the backwards compatibility package. I took the jar file and placed it in the libs folder of my project. I extended Fragment and then tried to inflate it by setting the contentView of the Activity to

<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.test.fragments.AdFragment"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/></LinearLayout>

But when I set the content view it fails with a ClassNotFoundException for the fragment tag. Here is the logcat output.

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: \
      android.view.InflateException: Binary XML file line #51: \
      Error inflating class fragment
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1777)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1793)
   at android.app.ActivityThread.access$1500(ActivityThread.java:123)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3848)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
   at dalvik.system.NativeStart.main(Native Method)

 Caused by: android.view.InflateException: Binary XML file line #51: \
      Error inflating class fragment
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:211)
   at android.app.Activity.setContentView(Activity.java:1657)
   at com.test.base.activities.TabbedStoreActivity.onCreate(TabbedStoreActivity.java:46)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1731)
   ... 11 more

 Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader \
      dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar: \
      /data/app/com.test.test.apk]
   at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
   at android.view.LayoutInflater.createView(LayoutInflater.java:471)
   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
   ... 20 more
JJD
  • 50,076
  • 60
  • 203
  • 339
Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • Similar problem: http://stackoverflow.com/questions/5510771/looking-for-wrong-fragment-class-with-android-compatibility-library. I had this issue because I wrote `Fragment` instead of `fragment` – Casebash Aug 17 '12 at 07:47

1 Answers1

76

Make sure your activity is inheriting from FragmentActivity, otherwise <fragment> does not work. Here is a sample project demonstrating this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks I will try this out tonight when I get home, crazy thing is I searched the docs and `FragmentActivity` isn't listed but then I checked the library and its in there alright. This puts a damper on my plans since there is no `FragmentListActivity`, etc. Makes it a whole lot of work at once rather than slowly moving stuff over to fragments. – Nathan Schwermann Mar 23 '11 at 17:29
  • Suck! Is this a limitation only in the compatibility library for android versions less than 3.0, or is this a limitation in android 3.0? – emmby Mar 23 '11 at 19:33
  • 1
    @emmby: The compatibility library needs this. If you are coding straight to the Android 3.0 fragment stuff, there is no `FragmentActivity`, as @schwiz points out. – CommonsWare Mar 23 '11 at 22:30
  • 5
    thanks Mark that was the problem, also when you extend FragmentActivity you must call super.onCreate before setting the content view. – Nathan Schwermann Mar 24 '11 at 02:44
  • 1
    That was it, saved me from pulling my hair out all afternoon – Philippe Girolami Jun 01 '11 at 13:59
  • Same issue here, not at all obvious this was the problem from the exception trace. – chrisr Oct 29 '11 at 03:08
  • @CommonsWare: Oh sweet Lord, how much that helped me. I'll start buying your books out of sheer respect for your genuine efforts to help us mere mortals :-) – dbm Dec 18 '11 at 11:32
  • This is not the sollution, AppCompatActivity extends android.support.v4.app.FragmentActivity yet the error appears. – f470071 Nov 04 '15 at 17:50
  • @f470071: Then you have some other problem. Feel free to [ask a separate Stack Overflow question](http://stackoverflow.com/questions/ask), where you provide [a minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) demonstrating your problem. – CommonsWare Nov 04 '15 at 17:53
  • @CommonsWare http://stackoverflow.com/questions/33533893/how-to-use-preferences-in-android-support-library-v7-rev23 – f470071 Nov 05 '15 at 09:33
  • @CommonsWare See what happens if you provide minimal, complete and verifiable example. – f470071 Nov 07 '15 at 16:50