【问题标题】:Android SDK error: Trying instantiate a class that is not a fragmentAndroid SDK错误:尝试实例化一个不是片段的类
【发布时间】:2025-12-31 18:15:01
【问题描述】:

我几乎不会尝试创建一个带有顶部菜单和下方可更改视图的简单应用程序(通过按下菜单片段中的按钮,我们可以更改下面片段的视图)。 因此,我在主视图中有 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

所以,这些是我的 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>

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

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

【问题讨论】:

  • 谢谢,成功了。 API、教程、示例,一切看起来都一团糟
  • 我会把它作为答案,这样未来的谷歌人就会看到它。

标签: android class sdk fragment instantiation


【解决方案1】:

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


编辑

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

【讨论】:

  • 或使用任何其他派生词,例如ActionBarActivity
  • 谢谢。我无法想象我要花多长时间才能找到那个。
【解决方案2】:

事实证明,我在 onCreate 中的操作顺序错误:

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

而不是

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      最近更新 更多