ScrollView does not work: ScrollView can host only one direct child

1

Good afternoon, I'm trying to add a ScrollView but it's not working, the xml file is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context="matgic.com.matgic.MainActivity"
>

<ScrollView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:scrollbars="vertical">

<TextView
   android:id="@+id/selidio"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#faf8f8"
   android:text="@string/sel_idio"
   android:layout_gravity="center"
   android:textSize="30sp"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   />

<TextView
   android:id="@+id/sellan"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#fcfbfb"
   android:text="@string/sel_lan"
   android:layout_gravity="center"
   android:textSize="30sp"
   android:layout_below="@id/selidio"
   android:layout_centerHorizontal="true"
  />


<ImageButton
   android:id="@+id/es"
   android:layout_width="150dp"
   android:layout_height="150dp"
   android:layout_alignParentLeft="true"
   android:layout_centerVertical="true"
   app:srcCompat="@drawable/espaniol"
/>

<ImageButton
   android:id="@+id/en"
   android:layout_width="150dp"
   android:layout_height="150dp"
   android:layout_alignParentRight="true"
   android:layout_centerVertical="true"
   app:srcCompat="@drawable/english"
/>
</ScrollView>

</RelativeLayout>

This error displays in Stack Trace :

  

java.lang.IllegalStateException: ScrollView can host only one direct child       at android.widget.ScrollView.addView (ScrollView.java:279)       at android.view.LayoutInflater.rInflate_Original (LayoutInflater.java:867)       at android.view.LayoutInflater_Delegate.rInflate (LayoutInflater_Delegate.java:72)       at android.view.LayoutInflater.rInflate (LayoutInflater.java:837)       at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:824)       at android.view.LayoutInflater.rInflate_Original (LayoutInflater.java:866)       at android.view.LayoutInflater_Delegate.rInflate (LayoutInflater_Delegate.java:72)       at android.view.LayoutInflater.rInflate (LayoutInflater.java:837)       at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:824)       at android.view.LayoutInflater.inflate (LayoutInflater.java:515)       at android.view.LayoutInflater.inflate (LayoutInflater.java:394)       at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate (RenderSessionImpl.java:325)       at com.android.layoutlib.bridge.Bridge.createSession (Bridge.java:384)       at com.android.tools.idea.layoutlib.LayoutLibrary.createSession (LayoutLibrary.java:193)       at com.android.tools.idea.rendering.RenderTask.createRenderSession (RenderTask.java:547)       at com.android.tools.idea.rendering.RenderTask.lambda $ inflate $ 3 (RenderTask.java:681)       at java.util.concurrent.FutureTask.run (FutureTask.java:266)       at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:617)       at java.lang.Thread.run (Thread.java:745)

    
asked by Sergio 29.01.2018 в 21:48
source

2 answers

1

That happens because the scroll view can only have one child

You in this case have the following children:

<TextView
   android:id="@+id/selidio"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#faf8f8"
   android:text="@string/sel_idio"
   android:layout_gravity="center"
   android:textSize="30sp"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   />

<TextView
   android:id="@+id/sellan"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#fcfbfb"
   android:text="@string/sel_lan"
   android:layout_gravity="center"
   android:textSize="30sp"
   android:layout_below="@id/selidio"
   android:layout_centerHorizontal="true"
  />


<ImageButton
   android:id="@+id/es"
   android:layout_width="150dp"
   android:layout_height="150dp"
   android:layout_alignParentLeft="true"
   android:layout_centerVertical="true"
   app:srcCompat="@drawable/espaniol"
/>

<ImageButton
   android:id="@+id/en"
   android:layout_width="150dp"
   android:layout_height="150dp"
   android:layout_alignParentRight="true"
   android:layout_centerVertical="true"
   app:srcCompat="@drawable/english"
/>

In total there are 4.

What you can do is put these four children into a Relative Layout, so that it is the only son of scroll view and the 4 children become children of the relative.

As follows:

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
<TextView
       android:id="@+id/selidio"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="#faf8f8"
       android:text="@string/sel_idio"
       android:layout_gravity="center"
       android:textSize="30sp"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       />

    <TextView
       android:id="@+id/sellan"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="#fcfbfb"
       android:text="@string/sel_lan"
       android:layout_gravity="center"
       android:textSize="30sp"
       android:layout_below="@id/selidio"
       android:layout_centerHorizontal="true"
      />


    <ImageButton
       android:id="@+id/es"
       android:layout_width="150dp"
       android:layout_height="150dp"
       android:layout_alignParentLeft="true"
       android:layout_centerVertical="true"
       app:srcCompat="@drawable/espaniol"
    />

    <ImageButton
       android:id="@+id/en"
       android:layout_width="150dp"
       android:layout_height="150dp"
       android:layout_alignParentRight="true"
       android:layout_centerVertical="true"
       app:srcCompat="@drawable/english"
    />

</RelativeLayout>

I hope it helps you:)

    
answered by 29.01.2018 / 21:51
source
1

The description of the error indicates the problem,

  

java.lang.IllegalStateException: ScrollView can host only one direct child

in Spanish it would be:

  

java.lang.IllegalStateException: ScrollView can host only a direct child

You can not add multiple elements within a ScrollView , instead you can add a container (layout) and it can contain several elements at the same time.

Example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context="matgic.com.matgic.MainActivity">

<ScrollView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:scrollbars="vertical">

    <RelativeLayout
        android:id="@+id/layout_contenedor"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

         // ... Agregar aquí elementos!!! 

    </RelativeLayout>

   </ScrollView>

</RelativeLayout>

This would be the code of the complete layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context="matgic.com.matgic.MainActivity">

<ScrollView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:scrollbars="vertical">

    <RelativeLayout
        android:id="@+id/layout_contenedor"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

         <TextView
   android:id="@+id/selidio"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#faf8f8"
   android:text="@string/sel_idio"
   android:layout_gravity="center"
   android:textSize="30sp"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"/>

<TextView
   android:id="@+id/sellan"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#fcfbfb"
   android:text="@string/sel_lan"
   android:layout_gravity="center"
   android:textSize="30sp"
   android:layout_below="@id/selidio"
   android:layout_centerHorizontal="true"/>


<ImageButton
   android:id="@+id/es"
   android:layout_width="150dp"
   android:layout_height="150dp"
   android:layout_alignParentLeft="true"
   android:layout_centerVertical="true"
   app:srcCompat="@drawable/espaniol"/>

<ImageButton
   android:id="@+id/en"
   android:layout_width="150dp"
   android:layout_height="150dp"
   android:layout_alignParentRight="true"
   android:layout_centerVertical="true"
   app:srcCompat="@drawable/english"/>

    </RelativeLayout>

   </ScrollView>

</RelativeLayout>
    
answered by 29.01.2018 в 22:51