我才刚刚开始学习片段,这个错误确实让我感到沮丧,我必须缺少一些简单的东西:

我在启动时收到错误消息,说Fragment类无法转换为android.app.Fragment-我不确定为什么

活动的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_blue" >

    <Button
        android:id="@+id/bStartTest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/btn_main"
        android:text="@string/start_test"
        android:textColor="@color/white"
        android:textSize="24sp" />

    <ProgressBar
        android:id="@+id/pbGetDataBase"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/tvHomeTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:padding="16dp"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="34sp" />

    <LinearLayout
        android:id="@+id/firstTimeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tvHomeTitle"
        android:background="@color/background_blue"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
         >

        <fragment
        android:id="@+id/overviewFragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        class="com.mycqs.cardiology.OverviewFragment" />

    </LinearLayout>

</RelativeLayout>


活动课:

public class HomeActivity extends SherlockActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    }
}


片段类:

public class OverviewFragment extends SherlockFragment {


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.temporary,
        container, false);
    return view;
  }

}


-

06-28 22:27:48.077: E/AndroidRuntime(23806): FATAL EXCEPTION: main
06-28 22:27:48.077: E/AndroidRuntime(23806): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycqs.cardiology/com.mycqs.cardiology.HomeActivity}: android.view.InflateException: Binary XML file line #87: Error inflating class fragment
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.os.Looper.loop(Looper.java:137)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.ActivityThread.main(ActivityThread.java:5293)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at java.lang.reflect.Method.invokeNative(Native Method)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at java.lang.reflect.Method.invoke(Method.java:511)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at dalvik.system.NativeStart.main(Native Method)
06-28 22:27:48.077: E/AndroidRuntime(23806): Caused by: android.view.InflateException: Binary XML file line #87: Error inflating class fragment
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:134)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:229)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at com.mycqs.cardiology.HomeActivity.onCreate(HomeActivity.java:58)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.Activity.performCreate(Activity.java:5250)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
06-28 22:27:48.077: E/AndroidRuntime(23806):    ... 11 more
06-28 22:27:48.077: E/AndroidRuntime(23806): Caused by: java.lang.ClassCastException: com.mycqs.cardiology.OverviewFragment cannot be cast to android.app.Fragment
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.Fragment.instantiate(Fragment.java:585)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.Fragment.instantiate(Fragment.java:560)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.app.Activity.onCreateView(Activity.java:4850)
06-28 22:27:48.077: E/AndroidRuntime(23806):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
06-28 22:27:48.077: E/AndroidRuntime(23806):    ... 23 more

最佳答案

SherlockActivity更改为SherlockFragmentActivity。如果要使用SherlockFragmentSherlockListFragment等,则需要将它们托管在SherlockFragmentActivity中。

关于android - 无法将SherlockFragment强制转换为Fragment,我的 Activity 是SherlockActivty,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17373802/

10-10 12:49