Android SDK错误:尝试实例化不是片段的类

时间:2021-08-16 17:06:29

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:

我很难创建一个简单的应用程序,顶部菜单和下面的可更改视图(通过按下菜单片段中的按钮,我们更改下面片段的视图)。所以,我在主视图中有2个片段,但是当我试图在模拟器中运行应用程序时,我得到一个错误:

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

So, these are my XML layouts:

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

main.xml

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

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

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;
    }
}

bottomcontent

bottomcontent

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;

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

3 个解决方案

#1


54  

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

您应该使用FragmentActivity而不是Activity,因为您在活动主体中使用支持片段和多个片段


Edit

编辑

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

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

#2


5  

In my case it turned out, I was doing stuff in onCreate in the wrong order:

在我的情况下,事实证明,我正在以错误的顺序在onCreate中做事:

setContentView(R.layout.activity_qr_code_scan);
super.onCreate(savedInstanceState);

instead of

代替

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_code_scan);

#3


1  

As you are using fragments in your layout and I suggest you to extend your class from fragment or fragment activity.

当您在布局中使用片段时,我建议您从片段或片段活动扩展您的类。

#1


54  

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

您应该使用FragmentActivity而不是Activity,因为您在活动主体中使用支持片段和多个片段


Edit

编辑

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

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

#2


5  

In my case it turned out, I was doing stuff in onCreate in the wrong order:

在我的情况下,事实证明,我正在以错误的顺序在onCreate中做事:

setContentView(R.layout.activity_qr_code_scan);
super.onCreate(savedInstanceState);

instead of

代替

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_code_scan);

#3


1  

As you are using fragments in your layout and I suggest you to extend your class from fragment or fragment activity.

当您在布局中使用片段时,我建议您从片段或片段活动扩展您的类。