Search code examples
androidandroid-fragmentsandroid-scrollview

IllegalStateException : ScrollView can host only one direct child


I have a fragment, which comprises of a scrollview which has a single LinearLayout(I have wrapped up other LinearLayouts into this)

Fragment_Details.xml

 <ScrollView
     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"
     tools:context=".DetailsActivityFragment">

     <LinearLayout...>

</ScrollView>

In the DetailsActivity i'm adding a fragment

DetailsActivity.java

 DetailsActivityFragment fragment = new DetailsActivityFragment();
        fragment.setArguments(b);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.movie_details,fragment)
                .commit();

Activity_Details.xml

<fragment
android:id="@+id/movie_details"
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:name=".DetailsActivityFragment"
tools:ignore="MergeRootFrame"/>

But when i run the program i get the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.IllegalStateException: ScrollView can host only one direct child

I have followed threads on the issue of ScrollView, but i'm not able to solve this. Help please!!


Solution

  • Use Frame Layout instead of fragment in activity_details bcoz fragment is used when u have a fixed fragment to show. Here u r showing ur movie detail fragment using it but then again u r inflating another fragment(another instance of movie detail fragment) in ur activity and creates the problem.