您的位置:首页 > 编程语言 > Java开发

静态注册Fragemnt

2016-05-25 17:07 417 查看
静态注册

Activity布局

<?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:baselineAligned="false"

    android:divider="?android:attr/dividerHorizontal"

    android:orientation="horizontal"

    android:showDividers="middle" >

    <fragment

        android:id="@+id/item_list"

        android:name="com.example.fragment.ItemListFragment"   //fragment类

        android:layout_width="0dip"

        android:layout_height="match_parent"

        android:layout_weight="1" />

    <fragment

        android:id="@+id/item_detail_container"

        android:name="com.example.fragment.ItemDetailFragment"   //fragemtn类

        android:layout_width="0dip"

        android:layout_height="match_parent"

        android:layout_weight="2" />

</LinearLayout>

Activtiy:

package com.example.myevenbus;

import org.androidannotations.annotations.EActivity;

import android.app.Activity;

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

//继承fragment

public class TwoActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

}

当使用静态方式注册fragment时造成下面的错误

java.lang.RuntimeException:Unable to start activityComponentInfo{com.example.sharecontent/com.example.sharecontent.MyFragment}:android.view.InflateException: Binary XML file line #7: Error inflating classfragment

原因是在布局中的<fragment>没有添加id,activity无法区分两个Fragment
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息