Home > Enterprise >  I keep getting an error: Caused by: java.lang.IllegalStateException: ScrollView can host only one di
I keep getting an error: Caused by: java.lang.IllegalStateException: ScrollView can host only one di

Time:01-11

Good Morning, I keep getting an error in my XML file that says android.view.InflateException: Binary XML file line #38: ScrollView can host only one direct child Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child.

How do i go about to start fixing this issue?

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="viewModel"
            type="com.exampleapplication.foodorder.functionalities.account.AccountViewModel"
            />
    </data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@ id/receivecontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".functionalities.orderlist.firsttime.ReceiveCodeFragment">

    <androidx.core.widget.NestedScrollView
        android:id="@ id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@ id/receivecontainer">

    <Button
        android:id="@ id/receivebackbtn"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/blackarrow"
        app:layout_constraintBottom_toTopOf="@ id/textView6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.051"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.695" />

    <TextView
        android:id="@ id/textView6"
        style="@style/Title24"
        android:layout_width="373dp"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="70dp"
        android:layout_height="60dp"
        android:text="@string/receiverestauranttitle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@ id/textView5"
        app:layout_constraintStart_toStartOf="@ id/textView5"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.076" />

    <TextView
        android:id="@ id/textView5"
        android:layout_width="350dp"
        android:layout_height="280dp"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="15dp"
        android:singleLine="false"
        android:text="@string/receiverestaurantfooter"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@ id/startterminal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView6"
        app:layout_constraintVertical_bias="0.057" />

    <TextView
        android:id="@ id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10sp"
        android:layout_marginTop="44dp"
        android:text="Email"
        android:textColor="@color/coldGrey9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.050"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView5" />

    <com.google.android.material.card.MaterialCardView
        android:id="@ id/cardView3"
        android:layout_width="310dp"
        android:layout_height="52dp"
        android:layout_marginStart="4dp"
        android:layout_marginRight="20dp"
        android:stateListAnimator="@null"
        android:textColor="@color/coldGrey8"
        app:cardBackgroundColor="@color/whiteColor"
        app:cardElevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.268"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView7"
        app:strokeColor="@color/coldGrey3"
        app:strokeWidth="2dp"
        tools:cardElevation="0dp">

        <EditText
            android:id="@ id/restaurantEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:background="@null"
            android:fontFamily="sans-serif"
            android:hint="Enter email here"
            android:stateListAnimator="@null"
            android:text="@{viewModel.profile.locationEmail}"
            android:textColor="@color/coldGrey8"
            android:textColorHint="@color/coldGrey5"
            android:textSize="14dp"
            android:textStyle="normal"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/cardView3" />

    </com.google.android.material.card.MaterialCardView>

    <Button
        android:id="@ id/sendHtmlcode"
        android:layout_width="330dp"
        android:layout_height="60sp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="48dp"
        android:backgroundTint="@color/coldGrey4"
        android:letterSpacing="0.03"
        android:text="@string/sendHtml"
        android:textAllCaps="false"
        android:textColor="@color/whiteColor"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/editText" />

</androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

What does it mean by directly only one child and it only occurs when i try to add a scrollview into the XML file into the application

CodePudding user response:

The error pretty much tells you everything you need to know. The ScrollView can only host a single child view in order for it to work properly. You need to wrap everything inside the scroll view in a container. Seeing that you use constraints for positioning the elements, you'll want to wrap them inside a ConstraintLayout.

CodePudding user response:

ScrollView can only have one direct child. It can't have multiple child Like you used.

Wrap all your views in a Linear Layout (or any other ie: Constraint, Relative) like this (Assuming all your views are vertically oriented):

<androidx.core.widget.NestedScrollView
        android:id="@ id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@ id/receivecontainer">

   <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <Button
        android:id="@ id/receivebackbtn"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/blackarrow"
        app:layout_constraintBottom_toTopOf="@ id/textView6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.051"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.695" />

    <TextView
        android:id="@ id/textView6"
        style="@style/Title24"
        android:layout_width="373dp"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="70dp"
        android:layout_height="60dp"
        android:text="@string/receiverestauranttitle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@ id/textView5"
        app:layout_constraintStart_toStartOf="@ id/textView5"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.076" />

    <TextView
        android:id="@ id/textView5"
        android:layout_width="350dp"
        android:layout_height="280dp"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="15dp"
        android:singleLine="false"
        android:text="@string/receiverestaurantfooter"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@ id/startterminal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView6"
        app:layout_constraintVertical_bias="0.057" />

    <TextView
        android:id="@ id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10sp"
        android:layout_marginTop="44dp"
        android:text="Email"
        android:textColor="@color/coldGrey9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.050"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView5" />

    <com.google.android.material.card.MaterialCardView
        android:id="@ id/cardView3"
        android:layout_width="310dp"
        android:layout_height="52dp"
        android:layout_marginStart="4dp"
        android:layout_marginRight="20dp"
        android:stateListAnimator="@null"
        android:textColor="@color/coldGrey8"
        app:cardBackgroundColor="@color/whiteColor"
        app:cardElevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.268"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView7"
        app:strokeColor="@color/coldGrey3"
        app:strokeWidth="2dp"
        tools:cardElevation="0dp">

        <EditText
            android:id="@ id/restaurantEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:background="@null"
            android:fontFamily="sans-serif"
            android:hint="Enter email here"
            android:stateListAnimator="@null"
            android:text="@{viewModel.profile.locationEmail}"
            android:textColor="@color/coldGrey8"
            android:textColorHint="@color/coldGrey5"
            android:textSize="14dp"
            android:textStyle="normal"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/cardView3" />

    </com.google.android.material.card.MaterialCardView>

    <Button
        android:id="@ id/sendHtmlcode"
        android:layout_width="330dp"
        android:layout_height="60sp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="48dp"
        android:backgroundTint="@color/coldGrey4"
        android:letterSpacing="0.03"
        android:text="@string/sendHtml"
        android:textAllCaps="false"
        android:textColor="@color/whiteColor"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/editText" />
    
    </LinearLayout>

</androidx.core.widget.NestedScrollView>

CodePudding user response:

Wrap all the views by LinearLayout/RelativeLayout/ConstraintLayout(or any other viewgroup as per view relations) and put that container viewgroup inside NestedScrollView like,

<NestedScrollView>
 
  <LinearLayout>

     //Other Views will go here...

  </LinearLayout>

</NestedScrollView>
  •  Tags:  
  • Related