1

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!!

Virat
  • 129
  • 4
  • 20
  • I hope as the error name suggest that scroll view should have only one child, u have placed only one linear layout within ur scroll view in Fragment_Details.XML. beyond that I am not completely sure but just try to use Frame Layout instead of fragment in activity_details bcoz fragment is used when u have a fixed fragment to show while here u r inflating another fragment again in ur activity. – Gaurav Jindal Apr 20 '16 at 05:15
  • Post your entire Fragment_Details layout content – shark1608 Apr 20 '16 at 08:58
  • Are you adding any other child to the ScrollView programatically using addView? – shark1608 Apr 20 '16 at 09:09
  • @shark1608 I'm not adding any other children to my scrollview anywhere in my program – Virat Apr 21 '16 at 07:22
  • @Gaurav jindal replacing fragment with FrameLayout as the parent view did solve the problem indeed... Thanks man... But I'm not sure if I can accept a comment as the accepted answer – Virat Apr 21 '16 at 07:25
  • @N00bs I have added my answer(with bit more explanation) so that other people can also understand what u did wrong. Glad I helped u – Gaurav Jindal Apr 21 '16 at 07:30

2 Answers2

2

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.

Gaurav Jindal
  • 227
  • 4
  • 17
1

What all views are there in your Linear Layouts? It seems that any of the layouts is having some child which implements it's own scrolling, like Listview.

See this: ListView inside ScrollView is not scrolling on Android

Community
  • 1
  • 1
T.M
  • 817
  • 11
  • 17
  • I did go through the thread of adding ListViews as I'm actually using LinearListView ( a custom class ) inside the LinearLayout ( inside ScrollView) .. The issue was with the root element that is replacing the root element (fragment) with FrameLayout did solve the problem :) – Virat Apr 21 '16 at 07:29