Android SDK 错误 : Trying instantiate a class that is not a fragment

标签 android class sdk fragment instantiation

我几乎不会尝试创建一个带有顶部菜单和下方可更改 View 的简单应用程序(通过按下菜单 fragment 中的按钮,我们可以更改下方 fragment 的 View )。 因此,我在主视图中有 2 个 fragment ,但是当尝试在模拟器中运行应用程序时,我收到如下错误:

Cause by android.app (bla bla bla, piece of crap Eclipse doesn't even allow copying the errors): 
Trying to instantiate a class com.example.android.topmenu that is not a fragment

所以,这些是我的 XML 布局:

ma​​in.xml

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

    <fragment
        android:id="@+id/menuFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:name="com.example.android.topmenu" >
    </fragment>

    <fragment
        android:id="@+id/contentFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="com.example.android.bottomcontent" >
    </fragment>

</LinearLayout>

topmenu.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="horizontal" >

   <Button
       android:id="@+id/Button1"
       android:layout_width="wrap_content"
       android:layout_height="match_parent" />

</LinearLayout>

bottom_content.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@+string/content_text" />

</LinearLayout>

这些是主要 Activity 和 fragment 的类

ma​​in_activity

package com.example.android;

import com.example.android.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class OLife extends Activity {
@Override
    public void onCreate(Bundle savedInstanceState) {
        // The activity is being created
        super.onCreate(savedInstanceState);
        // Set view
        setContentView(R.layout.main);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
        super.onDestroy();

        // Stop method tracing that the activity started during onCreate()
        android.os.Debug.stopMethodTracing();
    }
}

顶部菜单

package com.example.android;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

底部内容

package com.example.android;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
}

最佳答案

您应该使用 FragmentActivity 而不是 Activity 这是因为您在 Activity 主程序中使用了支持 Fragments 和多个 Fragment


编辑

您现在可以使用 Appcompat支持库并扩展 AppCompatActivity 以支持底层 api 的工具栏和 fragment 。

关于Android SDK 错误 : Trying instantiate a class that is not a fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24213756/

相关文章:

android - Fragment异步接口(interface)回调处理

java - 从另一个类访问组件

javascript - ES6 类 - 更新静态属性

java - 列出 Evernote 垃圾箱的内容

java - 当我更改 minSDKVersion 时发生 NetworkOnMainThreadException 错误

android - 如何在 android 中为我的折线创建方向箭头

android - 如何使用正确的用户代理显示 whatsapp

python - “NoneType”对象无法使用装饰器类调用

iphone sdk 3.2 到 4.0 Nib 编辑

java - 在 AVD 上运行时发生 Android 套接字错误