I get the Exception:
java.lang.IllegalStateException: ScrollView can host only one direct child
This is my layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg_listgrey"
android:scrollbars="none" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/bg_listgrey" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl1">
<ImageView
/>
<TextView
/>
<TextView
/>
<TextView
/>
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl2"" >
<ImageView
/>
<TextView
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
The same layout worked fine in case of an Activity. Where as it gives the exception when using in a Fragment.
MainActivity:
FragmentTransaction t = this.getSupportFragmentManager()
.beginTransaction();
t.add(R.id.frame, new mFragment());
t.commit();
Thank you
EDIT:
Bit if i wrap this ScrollView in a FragmeLayout, it works fine.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg_listgrey" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" >
.......
.......
.......
</ScrollView>
</FramLayout>
The layout will work just fine as the content view for an
Activity(but try to add another view to theScrollViewand see how it goes;) ) and it will work as well for aFragment, you just don’t use it well. This:will add the
Fragment'sview(the one created inonCreate) to theScrollView(theScrollViewhas the idR.id.frame), meaning that you’ll have two views in theScrollView(which is not allowed): theRelativeLayoutand the root of theFragment‘s view. TheaddViewmethods of theScrollViewclass will check to enforce that you end up with a single child in theScrollView.This is normal as you add the
Fragment‘s view to the parentFrameLayoutand not to theScrollView.