ScrollView 只能承载一个直接子级,但它只有一个

2024-05-09

每当我开始活动时,我都会收到此错误。

这是完整的堆栈跟踪:

Process: com.example, PID: 28799
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.activity.EditProjectActivity}: java.lang.IllegalStateException: ScrollView can host only one direct child
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754)
                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:145)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5938)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
                                                                 Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
                                                                    at android.widget.ScrollView.addView(ScrollView.java:416)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:822)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                                                                    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                    at com.example.activity.EditProjectActivity.onCreate(EditProjectActivity.java:38)
                                                                    at android.app.Activity.performCreate(Activity.java:6288)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754) 
                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:145) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5938) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

这是布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activity.EditProjectActivity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:orientation="horizontal"
                    android:id="@+id/ep_layout_name"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Project Name: "/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:id="@+id/ep_edit_name"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/ep_layout_name"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical"
                    android:id="@+id/ep_layout_categories"
                    >
                    <ListView
                        android:id="@+id/ep_list_categories"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                    <Button
                        android:id="@+id/ep_btn_add_category"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Add"
                        android:onClick="onAddCategoryButtonClick"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/ep_layout_categories"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical"
                    android:id="@+id/ep_layout_versions"
                    >
                    <ListView
                        android:id="@+id/ep_list_versions"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                    <Button
                        android:id="@+id/ep_btn_add_version"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Add"
                        android:onClick="onAddVersionButtonClick"
                        />
                </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

我明白该错误的含义,并且我已尝试相应地解决它,但它仍然抛出相同的异常。

ScrollView 只有一个直接子级,那就是RelativeLayout。我还做到了这一点,以便 ScrollView 有一个父视图,而不是基本视图,以防它导致异常,但它不起作用。


可能会尝试添加一个LinearLayout再次与vertical之前的定向RelativeLayout

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activity.EditProjectActivity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
<LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:orientation="horizontal"
                    android:id="@+id/ep_layout_name"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Project Name: "/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:id="@+id/ep_edit_name"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/ep_layout_name"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical"
                    android:id="@+id/ep_layout_categories"
                    >
                    <ListView
                        android:id="@+id/ep_list_categories"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                    <Button
                        android:id="@+id/ep_btn_add_category"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Add"
                        android:onClick="onAddCategoryButtonClick"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/ep_layout_categories"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical"
                    android:id="@+id/ep_layout_versions"
                    >
                    <ListView
                        android:id="@+id/ep_list_versions"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                    <Button
                        android:id="@+id/ep_btn_add_version"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Add"
                        android:onClick="onAddVersionButtonClick"
                        />
                </LinearLayout>
         </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ScrollView 只能承载一个直接子级,但它只有一个 的相关文章

  • 如何在 Android / Java 中获取图像的分辨率

    如何在 Android 中找到任何图像的分辨率 获取存储在磁盘中的图像大小的有效方法 例如获取用户选择上传的图像文件的大小 是使用BitmapFactory Options并设置inJustDecodeBounds为真 这样做你就会获取图像
  • TypedArray 的 getResourceId 方法

    我正在阅读有关的文档获取资源Id https developer android com reference android content res TypedArray html getResourceId int 20int 方法 它说
  • 与其他图标相比,AppCompat ShareActionProvider 图标太大

    我将 ActionBarSherlock 更改为 AppCompat v7 我已经完成了使其工作所需的所有更改 但是共享图标 使用 ShareActionProvider 发生了一些奇怪的情况 与其他图标相比 共享图标太大 我还使用支持库进
  • 对话框上的 EditText 不返回任何文本

    我太累了 找不到错误 我没有发现任何错误 但我没有从 editText 收到任何文本 请看下面的代码 活动密码 xml
  • GridView 中多次调用 getView()

    我的 Activity 由包含 40 多个元素的 GridView 组成 开始活动后 用户最多可以看到 15 个项目 3 行 每行 5 个项目 我在 getView 正文中编写传递给获取 View 的 LogCat 编号 Log i get
  • Android 自定义视图不能以正确的方式处理透明度/alpha

    我正在绘制自定义视图 在此视图中 我使用两个不同的绘画和路径对象在画布上绘画 我基本上是在绘制两个重叠的形状 添加 Alpha 后 视图中重叠的部分比图像的其余部分更暗 这是不希望的 但我不知道如何解决它 这是我的代码片段 用于展示我如何在
  • Retrofit-2 内容类型问题

    我的 Api 接受 Content Type application json 作为标头 我按照改造文档中所述完美设置了标题 Headers Content Type application json POST user classes C
  • Firestore - RecycleView - 图像持有者

    我不知道如何编写图像的支架 我已经设置了 2 个文本 但我不知道图像的支架应该是什么样子 你能帮我告诉我图像的文字应该是什么样子才能正确显示吗 holder artistImage setImageResource model getArt
  • Renderscript 示例构建错误

    所以我想尝试使用 RenderScript 的示例 并在 Eclipse 中导入了 HelloWorld 但它给了我这样的错误 2011 10 25 13 10 48 HelloWorld home mileoresko workspace
  • 如何持续更新MPAndroidChart中的Y轴值

    我希望 LineChart 中的轴能够实时调整其最大值和最小值 当新数据的 Y 值增加 正值和负值 时 像 ResetAxisMaxValue 和 ResetAxisMinValue 这样的函数可以很好地工作 但是 一旦信号再次变低 Y 值
  • 如何使用 Firebase UI 在 recyclerView 中显示时从 Firebase 数据库中的子级引用父级

    我目前正在引用 quote text 子项 这些数据显示在recyclerview using firebase用户界面 我想获取 quote text 的父名称 如何获取 quote text 孩子的父母姓名 When user clic
  • 导航抽屉默认片段

    我是一名新手开发人员 我正在将导航抽屉与 android support v7 集成到我的应用程序中 我有一个问题 当我启动应用程序时 主要布局是这样的
  • 以编程方式将 TextView 添加到主屏幕小部件

    我想以编程方式将文本视图控件添加到我的主屏幕小部件 在下面的示例中 我使用 TextView 填充 Linearlayout 但是这里应该如何使用 RemoteView 它只接受 xml 资源布局作为参数 public class MyWi
  • 为什么Android应用程序在发布到市场后尺寸会增加?

    我最近在 Android 市场上发布了我的应用程序 显示应用程序大小为 5 4MB 而实际 apk 大小为 2 8MB 为什么显示多出2MB 我应该如何限制我的应用程序大小 请帮我 您的应用程序大小会增加 因为您使用了复制保护选项ON在发布
  • Android - 如何更改 TimePicker 中的文本颜色?

    我正在使用 TimePicker 到 LinearLayout 中 背景颜色 黑色 但是 我看不到 TimePicker 中的数字 并且我需要在布局中将背景颜色设置为黑色 如何更改 TimePicker 中的 textColor 我已经尝试
  • Espresso 和 Proguard 的 Java.lang.NoClassDefFoundError

    我对 Espresso 不太有经验 但我终于成功地运行了它 我有一个应用程序需要通过 Proguard 缩小才能处于 56K 方法之下 该应用程序以 3 秒的动画开始 因此我需要等到该动画结束才能继续 这就是我尝试用该方法做的事情waitF
  • android httprequest java.net.UnknownHostException

    我想用android发出http请求 是使用这个 void testHTTP HttpClient httpClient new DefaultHttpClient HttpUriRequest request new HttpPost h
  • 按字母顺序过滤 Firestore 数据以对 Google Cloud 中的文档读取进行分类/减少

    基于这样的事实Cloud Firestore 不支持全文搜索 https firebase google com docs firestore solutions search到目前为止 我决定问这个question https stack
  • 在android中跟踪FTP上传数据?

    我有一个运行 Android 的 FTP 系统 但我希望能够在上传时跟踪字节 这样我就可以在上传过程中更新进度条 安卓可以实现这个功能吗 现在 我正在使用org apache common net ftp我正在使用的代码如下 另外 我在 A
  • 当ScrollView滚动到底部时加载更多数据

    我有一个带有动态加载内容的滚动视图 有时可能会有很多内容 所以我想在用户滚动到底部时加载更多内容 我搜索了合适的方法 发现了两种 onScrollChanged and getScrollY 但我不知道如何将它用于我的目的 请给我一些建议

随机推荐