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

Viewed 14762

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)
8 Answers

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

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>.

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

Vejam esse post abaixo o comentário do Mike M, o SnackBar provoca isso também. No meu caso, substitui por Toast, por enquanto - até solução definitiva. Exception in Scrollview

Related