用兼容包android充气片段

时间:2011-03-23 07:34:26

标签: android backwards-compatibility android-fragments android-inflate

我正在尝试使用向后兼容包来扩展包含Fragment的布局。我把jar文件放在我项目的libs文件夹中。我扩展了Fragment,然后尝试通过将Activity的contentView设置为

来使其膨胀
<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>

但是当我设置内容视图时,它会因片段标记的ClassNotFoundException而失败。 这是logcat输出。

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

1 个答案:

答案 0 :(得分:76)

确保您的活动继承自FragmentActivity,否则<fragment>不起作用。 Here is a sample project证明了这一点。