尝试使用SherlockListFragment时,为什么会出现“无法转换为android.app.Fragment”错误

时间:2012-10-12 07:04:18

标签: android android-fragments actionbarsherlock

我必须在我的应用程序中将ListFragments转换为SherlockListFragment。

因此,当我正确设置Sherlock时(至少它在我的主屏幕上作为SherlockActivity工作)我现在只需要使用SherlockListFragment而不是ListFragment。

所以我所做的就是将我的fragemnt从ListFragment改为SherlockListFragment:

    public class Select_F extends SherlockListFragment { //before it was ListFragment

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
  }
...

我在xml中调用它:(我没有修改)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment                             <- this is line 7
        android:id="@+id/f"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.xxx.Screens.Select_F" >
    </fragment>

</LinearLayout>

然后在充气期间我得到施法错误。

10-12 08:49:07.420: E/AndroidRuntime(15144): FATAL EXCEPTION: main
10-12 08:49:07.420: E/AndroidRuntime(15144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.xxx.Select_screen}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.os.Looper.loop(Looper.java:137)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.ActivityThread.main(ActivityThread.java:4424)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at java.lang.reflect.Method.invokeNative(Native Method)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at java.lang.reflect.Method.invoke(Method.java:511)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at dalvik.system.NativeStart.main(Native Method)
10-12 08:49:07.420: E/AndroidRuntime(15144): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:229)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at com.xxx.xxx.Select_screen.onCreate(Select_screen.java:62)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.Activity.performCreate(Activity.java:4465)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-12 08:49:07.420: E/AndroidRuntime(15144):    ... 11 more

10-12 08:49:07.420: E/AndroidRuntime(15144): Caused by: java.lang.ClassCastException: com.xxx.Screens.Select_F cannot be cast to android.app.Fragment

10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.Fragment.instantiate(Fragment.java:574)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.Fragment.instantiate(Fragment.java:549)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.app.Activity.onCreateView(Activity.java:4235)
10-12 08:49:07.420: E/AndroidRuntime(15144):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
10-12 08:49:07.420: E/AndroidRuntime(15144):    ... 22 more

我想某种程度上xml文件仍然需要Fragment而不是SherlockListFragment - 但为什么呢?

我在这里想念的是什么?

非常感谢!

1 个答案:

答案 0 :(得分:2)

Sherlock片段基于兼容性包(Fragment)中的android.support.v4.app.Fragment类,因此它们要求您的活动扩展FragmentActivity(也来自兼容包)或SherlockActivity中的一个扩展FragmentActivity