edit (2nd answer):
I’ve been playing with the code and I’m still not sure WHY this happens, definitely there’s something wrong on the fragment manager when it swapping fragments (replace(resId, fragment))
In my fragment onCreateView was this code:
View view = inflater.inflate(R.layout.post_fragment, null, false);
if (item == null)
return view;
return BuildView(view);
post_fragment is an empty canvas with 1 ScrollView, 1 LinearLayout inside it, and some other ViewGroups inside it. This is to load an empty canvas on the 1st load (item is null) and on the 2nd time, through a user input, it would get the actual data to show.
for some reason beyond my knowledge, as seen in the logcat, FragmentManagerImpl.moveToState was calling ScrollView.AddChild causing the error.
Now I changed my fragments onCreateView to:
if (item == null)
return inflater.inflate(R.layout.empty, null, false);
View view = inflater.inflate(R.layout.post_fragment, null, false);
return BuildView(view);
so that the ’empty’ canvas is a FrameLayout with nothing on it, and now everything is working happily.
I’m sure this sounds like some odd error on the actual fragments framework, but it’s a work around it.
1st answer:
Raptrex, as far as I can see. Yes, that table layout is the only child your scrowview have. I am having the exactly the same problem, and very frustrated trying to solve it.
From the LogCat/Debug, it seems that there’s something in the Fragments manager that is calling and .AddView(view) in my ScrollView (check the log) it depends on how I change my XML layout I get this error or not, which let’s say sounds pretty absurd!
Any ideas of how?why?what? I’m listening…
FATAL EXCEPTION: main
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:219)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:787)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
at android.app.BackStackRecord.run(BackStackRecord.java:638)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1309)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:398)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
When you run your project sometimes you get ‘Scrollview can host only one direct child’ error like this in android studio logcat.
So today we are going to solve this type of logcat error in android studio.
What exactly error occurs in logcat while running the app?
While running app on android studio app crashes and error as below logcat.
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.view.InflateException: Binary XML file line #31 in com.example.myapplication:layout/activity_binary_convert: ScrollView can host only one direct child
Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:379)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.myapplication.BinaryConvert.onCreate(BinaryConvert.java:12)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
How to solve ‘ScrollView can host only one direct child’ logcat error in Android Studio.
This error is caused by undefined scroll view in XML file. Just change scrollview. Mostly this error occurs when you add scrollview at any place.
The solution is to change the scrollview direction or add proper orientation and width or height. Because here error is ‘ ScrollView can host only one direct child’.
‘host only one direct child’. Manage all layout properly as well under the scrollview.
posted
using ScrollView for scrollable layout
and
<ScrollView
android:layout_width="match_parent"
android:layout_height="200dp"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Here some sub elements
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Here some sub elements
</LinearLayout>
</ScrollView>
but getting error scrollview can host only one direct child
how to solve this scrollview can host only one direct child error ?
jaman
answered Nov 30 ‘-1 00:00
answered Nov 30 ‘-1 00:00
you can put LinearLayout or RelativeLayout or any layout to direct one children at ScrollView and all those children goes to under LinearLayout or RelativeLayout , instead of direct under ScrollView
<ScrollView
android:layout_width="match_parent"
android:layout_height="200dp" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECEFF1"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="end">
<androidx.core.widget.NestedScrollView
android:layout_width="match parent"
android:layout_height="match parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="#ED3733">
<ImageView
android:id="@+id/homeScreenNavigation"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_baseline_menu_24" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher_round" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:text="Dashboard"
android:textSize="40sp" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="16dp"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="16dp"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="16dp"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="16dp"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
</androidx.cardview.widget.CardView>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start|left"
app:menu="@menu/drawer_menu" />
</androidx.core.widget.NestedScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
Here «ScrollView can host only one direct child»
I don’t know how to solve this error can someone help me to solve this problem
Thank you……………………………,…………………………………………………………………………………………………………………….
- java
- android
- xml
- android-recyclerview
- scrollview
asked Jan 17, 2021 at 7:35
1
-
As the error says scroll view should have only one direct child, but have a
LinearLayout
and thenNavigationView
. Either move theNavigationView
inside theLinearLayout
or out of theScrollView
Jan 17, 2021 at 7:58
1 Answer
ScrollView and NestedScrollView can have only one child
you can use ConstraintLayout or other ViewGroup in ScrollView or NestedScrollView.
good luck.
answered Jan 17, 2021 at 8:16
I have multiple LinearLayout
s with a combined height that easily exceeds a device’s screen height. So in order to make my layout scrollable, I tried adding in a ScrollView
, but unfortunately I get the following error:
Scrollview can host only one direct child
I did some research, but couldn’t really make sense of how to solve it, even after reading a couple of solutions. I tried a few changes, but nothing worked for me so far. Any help?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20dip">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 1"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q1_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 2"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q2_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 3"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q3_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 4"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q4_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 5"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q5_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 6"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q6_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 7"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q7_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 8"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q8_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 9"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q9_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 10"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q10_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 11"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q11_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 12"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q12_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 13"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q13_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 14"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q14_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 15"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q15_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 16"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q16_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 17"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q17_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 18"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q18_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 19"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q19_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 20"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/q20_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Score:"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="@+id/final_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0/5"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
This question is related to
android
android-layout
android-scrollview
Wrap all the children inside of another LinearLayout with wrap_content
for both the width and the height as well as the vertical orientation.