27

ScrollView在我的 Android 布局中使用,但它显示

IllegalStateException "Scrollview can host only one direct child"

我怎样才能避免这个异常?

崩溃信息:

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)
4

7 回答 7

54

您添加了多个控件作为ScrollView. 如果要执行此操作,请将 a 添加LinearLayout为直接子项并将其他控件放入其中。

于 2010-09-17T12:00:32.490 回答
5

您可以IllegalStateException :Scollview can host only one direct child通过在滚动视图中仅托管一个直接子级来避免

于 2010-09-17T20:35:38.137 回答
4

我遇到相同异常的问题是我试图向用户显示 Snackbar 警告,并将视图作为参数来显示它。该视图位于滚动视图内部,并且以某种方式破坏了层次结构。当我切换回 Toast 时,异常消失了。

于 2017-05-29T12:44:59.197 回答
3

ScrollView 内部必须只包含一个布局,否则它包含多个布局或视图会出错。

此示例代码解决了您的问题,

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

下面的示例代码错误

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

父布局必须

于 2018-05-13T12:14:39.327 回答
0

如果您使用片段和 SlidingPane,请检查您是否在 SlidingPane 的 xml 布局中使用标签,然后在运行时在 SlidingPane 的 oncreateView 上替换并添加片段。=]

于 2014-06-05T01:13:16.280 回答
0

您只需要添加一个子项即可滚动布局不超过此。

于 2018-07-19T11:08:48.137 回答
0

这发生在我使用 Fragments 并将 ScrollView 作为 Fragment 的根视图时。问题是意外地将 Fragment 实例化为<fragment>XML 中的元素;这是一个错误,因为它实例化了两次,一次是在扩展布局 XML 时,一次是在我的应用程序代码中。<fragment>更改为后已修复<FrameLayout>

于 2018-10-26T01:51:15.280 回答