Android SDK错误:尝试实例化不是片段的类 [英] Android SDK error: Trying instantiate a class that is not a fragment

查看:82
本文介绍了Android SDK错误:尝试实例化不是片段的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎没有尝试创建一个带有顶部菜单和下方可更改视图的简单应用程序(通过按菜单片段中的按钮,我们可以更改以下片段的视图)。
因此,我在主视图中有2个片段,但是当尝试在模拟器中运行该应用程序时,出现类似以下的错误:

I am hardly trying to create a simple application with a top menu and a changeable view below (by pressing the buttons in the menu fragment we change the view of the fragment below). So, I have 2 fragments inside the main view but when trying to run the application in the emulator I get an error like:

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布局:

So, these are my XML layouts:

main.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>

这些是主要活动和片段的类

and these are the classes for the main activity and the fragments

main_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();
    }
}

topmenu

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 ,这是因为您在活动主体中使用了支持片段和多个片段

You should be using FragmentActivity instead of Activity that is because you are using support Fragments and multiple Fragments in your activity main

编辑

您现在可以使用 Appcompat 支持库并扩展 AppCompatActivity 支持工具栏和较低API的片段。

You can now use the Appcompat support library and extends AppCompatActivity to support toolbar and fragment for lower api.

这篇关于Android SDK错误:尝试实例化不是片段的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆