<merge /> can be used only with a valid ViewGroup root and attachToRoot=true

I'm a beginner in using fragments. Is it possible to have a layout like this:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <include layout="@layout/waiting_dialog"/>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@drawable/bg_tile"
        android:gravity="center_horizontal" 
        android:orientation="vertical"
        android:visibility="invisible">

        <include layout="@layout/no_entries"/>      
    </LinearLayout>
</merge>

and use it in the fragments' onCreateView like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.main, container, false); 
    return view;
}

There's always the following error:

07-11 09:53:47.608: E/AndroidRuntime(5602): android.view.InflateException: \
  <merge /> can be used only with a valid ViewGroup root and attachToRoot=true

How to handle this problem?

share | edit
   

3 Answers

Apparently as stated here it is not possible.

What I did was altogether drop the "root layout" (see battery_details.xml). So I create a fragment layout for my MonitorActivity :

public class MonitorActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor);
    }
}

where the layout is :

<!-- activity_monitor.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MonitorActivity" >
    <fragment android:name="di.k23b.hw3.fragments.MonitorDetailsFragment"
            android:id="@+id/monitor_details"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="di.k23b.hw3.fragments.MonitorPrefsFragment"
            android:id="@+id/monitor_preferences"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

and in MonitorDetailsFragment class :

public class MonitorDetailsFragment extends Fragment {  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.battery_details, container, false); // NEVER TRUE !
    }
}

where the battery_details.xml (created in res/layout) :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/batteryTextHealth"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="BATTERRRYYY"
    android:textIsSelectable="true" >
</TextView>

Works. So maybe you can edit the layout you use the fragments, add the <LinearLayout> there and just put the elements of the <LinearLayout> directly into the layout you use in the OnCreateView - dropping the merge tags


在<merge>中无法使用inflater.infata();;;; 
把你的布局换为Relatilayout 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值