29

I am using ScrollView in my Android Layout but it shows

IllegalStateException "Scrollview can host only one direct child"

How can I avoid this exception?

Crash info:

07-19 15:58:22.308 21372 21372 D AndroidRuntime: Shutting down VM
07-19 15:58:22.313 21372 21372 E AndroidRuntime: FATAL EXCEPTION: main
07-19 15:58:22.313 21372 21372 E AndroidRuntime: Process: com.languoguang.helloworld, PID: 21372
07-19 15:58:22.313 21372 21372 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.languoguang.helloworld/com.languoguang.helloworld.plugin_homeadd.activity.AddDeviceWifiSettingActivity}: android.view.InflateException: Binary XML file line #254: ScrollView can host only one direct child
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3190)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3285)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread.-wrap12(Unknown Source:0)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:108)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:166)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:7412)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
07-19 15:58:22.313 21372 21372 E AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #254: ScrollView can host only one direct child
07-19 15:58:22.313 21372 21372 E AndroidRuntime: Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.widget.ScrollView.addView(ScrollView.java:292)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.rInflate(LayoutInflater.java:878)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.rInflate(LayoutInflater.java:877)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.rInflate(LayoutInflater.java:877)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:477)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at com.android.internal.policy.HwPhoneWindow.setContentView(HwPhoneWindow.java:313)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.Activity.setContentView(Activity.java:2843)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at com.languoguang.helloworld.plugin_homeadd.activity.AddDeviceWifiSettingActivity.onCreate(AddDeviceWifiSettingActivity.java:82)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:7358)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3285)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread.-wrap12(Unknown Source:0)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:108)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:166)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:7412)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
07-19 15:58:22.313 21372 21372 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
Languoguang
  • 2,166
  • 2
  • 10
  • 15
mohammedsuhail
  • 701
  • 2
  • 11
  • 23

7 Answers7

57

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.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • I have added two android design support library widgets as direct child of scrollview and this was the cause of crash but resolved by putting them in the parent linearlayout – cammando Mar 19 '17 at 10:51
4

You can avoid IllegalStateException :Scollview can host only one direct child by only hosting one direct child in your scrollview

Falmarri
  • 47,727
  • 41
  • 151
  • 191
4

My problem with same exception was that I tried to show a Snackbar warning to user with a view as a parameter to show it. That view was inside scrollview, and it was breaking the hierarchy somehow. When i switched back to Toast, exception gone away.

shtolik
  • 1,341
  • 22
  • 29
3

inside ScrollView must contain only one layout otherwise its contain multiple layouts or views it get error.

This sample code solve your problem,

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  <!-- here add your views -->

    </LinearLayout>
</ScrollView>

Sample below code wrong

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

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

    </LinearLayout>
</ScrollView>

Parent layout must

Karthik Kompelli
  • 2,104
  • 1
  • 19
  • 22
1

This happened for me when using Fragments and having ScrollView as root view of the Fragment. The problem was accidentally instantiating the Fragment into a <fragment> element in XML; this is an error because it was instantiating it twice, once when inflating the layout XML and once in my app code. It was fixed after changing <fragment> to <FrameLayout>.

TalkLittle
  • 8,866
  • 6
  • 54
  • 51
0

If you are using fragments and SlidingPane, check if you are using tags in your xml layout of your SlidingPane, then replace by and add your fragments at runtime on oncreateView of your SlidingPane. =]

Charleston
  • 1,539
  • 18
  • 10
0

You have to add only one child to scroll layout not more than that .

Deven
  • 684
  • 7
  • 10