How to Avoid "Illegalstateexception: Scrollview Can Host Only One Direct Child"

ScrollView can host only one direct child with relativelayout

The Answer given by Md.ibrahim khalil is absolutely right,

Try by replacing your layout with this,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

//Scroll view starts here

<include
android:id="@+id/toolbar"
layout="@layout/view_toolbar" />

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

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/grey_868686">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="Most Download"
android:textColor="@color/white"
android:textSize="14sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:onClick="onOtherVideo"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="More"
android:textColor="@color/white"
android:textSize="14sp" />

</RelativeLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_video_terbaru"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/grey_868686">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="Funny Video"
android:textColor="@color/white"
android:textSize="14sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:onClick="onOtherVideoFunny"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="More"
android:textColor="@color/white"
android:textSize="14sp" />

</RelativeLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_funny_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/grey_868686">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="Religy Video"
android:textColor="@color/white"
android:textSize="14sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:onClick="onOtherReligyVideo"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="More"
android:textColor="@color/white"
android:textSize="14sp" />

</RelativeLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_religi_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/grey_868686">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="Others Video"
android:textColor="@color/white"
android:textSize="14sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:onClick="onOtherMoreVideo"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="More"
android:textColor="@color/white"
android:textSize="14sp" />

</RelativeLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_others_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>

IllegalStateException : ScrollView can host only one direct child

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.

IllegalStateException : scrollview can host only one direct child in one LinearLayout adding

You just need to remove this line: scroll.addView(layout); You already have this declared in xml and are trying to add it again, that's why you are getting the multiple children exception.

Try to run this:

public void addFiles() {
HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles);
LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles);
if(!FileManagerActivity.getFinalAttachFiles().isEmpty()) {
for (File file: FileManagerActivity.getFinalAttachFiles()) {

View line = new View(this);

line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView = new TextView(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(12);
informationView.setCompoundDrawablesWithIntrinsicBounds(
0, R.drawable.file_icon, 0, 0);
informationView.setText(file.getName().toString());
layout.addView(informationView, 0);
layout.addView(line, 1);

}
// This line is telling the system to add your LinearLayout to the ScrollView when it is already there, declared in your xml layout file
//scroll.addView(layout);
}

}

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

Just as the error says

ScrollView can host only one direct child

Wrap the Views inside of a LinearLayout so the ScrollView has only the LinearLayout as a direct child.

From the Docs

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
// all the views currently in your ScrollView
</LinearLayout>
</ScrollView>

ScrollView can host only one direct child exception

this may help.. it tells so because ScrollView cannot hold more than 1 child.. it needs single child which hosts all other view..

<ScrollView>
<LinearLayout
android:id="@+id/child">
<ImageView/>
...
...
</LinearLayout>
</ScrollView>

in your case

LinearLayout child = (LinearLayout)findViewById( R.id.child);
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( "PATH" ) );
iv.setScaleType( ScaleType.CENTER_INSIDE );
child.addView( sv );

.IllegalStateException: ScrollView can host only one direct child

No, you have one SrollView inside a LinearLayout. Your ScrollView just has a bunch of TextViews in it. Change it to this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:background="#ffffff"
tools:context=".Blood">

<TextView
android:layout_width="wrap_content"
android:layout_height="56dp"
android:textStyle="bold"
android:background="#f4051d"
android:textAlignment="center"
android:textColor="#ffffff"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Blood Type: Facts and Information"/>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:padding="5dp">

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

<TextView
android:id="@+id/intro"
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000000"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="When "/>

<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0e06f2"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Blood Types ( Groups )"/>

<TextView
android:id="@+id/blood_types"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="A blood "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<ImageView
android:id="@+id/image_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/drawable_blood_one"/>

<TextView
android:id="@+id/blood_type_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="In addition ."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0e06f2"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Donating Blood By Compatible Type"/>

<TextView
android:id="@+id/donate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="Blood "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<ImageView
android:id="@+id/image_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/drawable_blood_two"/>

<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0e06f2"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Finding Out Your Blood Type"/>

<TextView
android:id="@+id/find_blood_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="It is easy "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<ImageView
android:id="@+id/image_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/drawable_blood_three"/>

<TextView
android:id="@+id/find_blood_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="In most"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0e06f2"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="What Does Blood Group RH Factor Mean"/>

<TextView
android:id="@+id/rh_factor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="RH factor ."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0e06f2"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="What is the Rarest Blood Type"/>

<TextView
android:id="@+id/rare_blood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="According "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0e06f2"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Blood Type Diet"/>

<TextView
android:id="@+id/blood_type_diet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text=" The Ea"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0e06f2"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Blood Types and Personality"/>

<TextView
android:id="@+id/personality"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="Legend"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#0e06f2"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Facts: Blood Types"/>

<TextView
android:id="@+id/facts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed"
android:text="Blood"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />

</LinearLayout>

</ScrollView>
</LinearLayout>

How can I avoid IllegalStateException: Scrollview can host only one direct child?

You have added more than one controls as children of a ScrollView. If you want to do this add a LinearLayout as a direct child and put the other controls in it.

Scrollview can host only one direct child

Wrap all the children inside of another LinearLayout with wrap_content for both the width and the height as well as the vertical orientation.



Related Topics



Leave a reply



Submit