Android 为什么在xml中集成一个片段会失败,错误消息是无法将片段转换为视图?

Android 为什么在xml中集成一个片段会失败,错误消息是无法将片段转换为视图?,android,android-fragments,Android,Android Fragments,我想创建一个片段,可以在多个活动中用作backbutton。我知道Android有自己的backbutton,但有一个特定的UI对我来说很重要 我尝试通过添加我的xml将backbutton集成到AppCompatActivity中: <android.support.v4.app.Fragment android:name="com.myapp.uielements.BackButton" android:id="@+id/backbutton" android:

我想创建一个片段,可以在多个活动中用作backbutton。我知道Android有自己的backbutton,但有一个特定的UI对我来说很重要

我尝试通过添加我的xml将backbutton集成到AppCompatActivity中:

<android.support.v4.app.Fragment
    android:name="com.myapp.uielements.BackButton"
    android:id="@+id/backbutton"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>
我得到一个错误:

 Caused by: android.view.InflateException: Binary XML file line #48: Binary XML file line #48: Class is not a View android.support.v4.app.Fragment
 Caused by: android.view.InflateException: Binary XML file line #48: Class is not a View android.support.v4.app.Fragment
 Caused by: java.lang.ClassCastException: class android.support.v4.app.Fragment cannot be cast to android.view.View
android.support.v4.app.Fragment是一个不是视图的类名。布局充气机希望看到视图或特殊标记,如碎片。要通过XML布局实例化片段,请使用片段:

 Caused by: android.view.InflateException: Binary XML file line #48: Binary XML file line #48: Class is not a View android.support.v4.app.Fragment
 Caused by: android.view.InflateException: Binary XML file line #48: Class is not a View android.support.v4.app.Fragment
 Caused by: java.lang.ClassCastException: class android.support.v4.app.Fragment cannot be cast to android.view.View
<fragment android:name="com.myapp.uielements.BackButton" ...