【问题标题】:Android Fragment - Instantiation Exception in LayoutAndroid Fragment - 布局中的实例化异常
【发布时间】:2014-05-02 12:40:45
【问题描述】:

我有一个布局,其中包含一个名为 MenuFragment 的 <fragment>。但是,每当我启动应用程序时,我都会收到 Trying to instantiate a class com.usmaan.whackem.MenuFragment that is not a Fragment

布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/layoutHeader"
    android:layout_marginTop="20dp">

    <fragment android:name="com.usmaan.whackem.MenuFragment"
        android:id="@+id/menu_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

片段:

public class MenuFragment extends Fragment{

    public MenuFragment() {
    }

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

我试过了

1) 给&lt;fragment&gt;添加一个ID

2) 为&lt;fragment&gt;添加命名空间

【问题讨论】:

  • 贴出片段类的包名
  • 包 com.usmaan.whackem;
  • 什么是com.usmaan.myApp.MenuFragment?? myappwhackem!
  • 哦,对不起。忽略那个。意思是whackem。我自己改变了这一点。我现在就改一下。
  • 您的活动扩展了什么并为片段发布导入

标签: android layout fragment


【解决方案1】:

我通过扩展包含来自FragmentActivity 而不是Activity 的片段的活动来解决此问题。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多