<merge /> can be used only with a valid ViewGroup root and attachToRoot=true
2013-06-08 17:48
4741 查看
2down votefavorite | 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? android android-layout android-fragments
| |||
3 Answers
activeoldestvotesup vote1down vote | 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 |
把你的布局换为Relatilayout
相关文章推荐
- <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
- android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRo
- android.view.InflateException: <include /> can only be used inside of a ViewGroup
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- Android 开发 Tip 15 -- can be replaced by one <TextView/> and a compound drawable
- android This tag and its children can be replaced by one <TextView/> and a compound drawable 布局文件提示
- 关于 This tag and its children can be replaced by one <TextView/> and a compound drawable 的问题
- java.lang.IllegalStateException: ActionBarContextView can only be used with android:layout_width="
- WLS Exception: <BEA-149132> <Split-dir application HRSystem can only be deployed to the Admin Server
- A TextureView or a subclass can only be used with hardware acceleration enabled.错误原因分析
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- android xml文件中出现如下提醒:This tag and its children can be replaced by one <TextView/> and a compound drawable
- vs2005下出现“Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive”的解决方法
- 使用asp.net urlrewrite 出现 Session state can only be used when enableSessionState is set to true 错误
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- eclipse提示:This tag and its children can be replaced by one <TextView/> and a compound drawable