Reputation: 24012
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>
Upvotes: 0
Views: 883
Reputation: 87074
The same layout worked fine in case of an Activity. Where as it gives the exception when using in a Fragment.
The layout will work just fine as the content view for an Activity
(but try to add another view to the ScrollView
and see how it goes;) ) and it will work as well for a Fragment
, you just don't use it well. This:
t.add(R.id.frame, new mFragment());
will add the Fragment's
view(the one created in onCreate
) to the ScrollView
(the ScrollView
has the id R.id.frame
), meaning that you'll have two views in the ScrollView
(which is not allowed): the RelativeLayout
and the root of the Fragment
's view. The addView
methods of the ScrollView
class will check to enforce that you end up with a single child in the ScrollView
.
Bit if i wrap this ScrollView in a FragmeLayout, it works fine.
This is normal as you add the Fragment
's view to the parent FrameLayout
and not to the ScrollView
.
Upvotes: 1