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

我很难创建一个简单的应用程序,顶部菜单和下面的可更改视图(通过按菜单片段中的按钮我们更改下面片段的视图) . 所以,我在主视图中有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布局:

main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/menuFragment"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:name="com.example.android.topmenu" >

android:id="@+id/contentFragment"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:name="com.example.android.bottomcontent" >

topmenu.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:id="@+id/Button1"

android:layout_width="wrap_content"

android:layout_height="match_parent" />

bottom_content.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="10dp"

android:orientation="vertical">

android:id="@+id/textView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@+string/content_text" />

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

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

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值