gpt4 book ai didi

java - 实现 ScrollView 导致致命异常

转载 作者:行者123 更新时间:2023-12-01 17:21:35 25 4
gpt4 key购买 nike

我有一个RelativeLayout,我正在尝试添加向下滚动的功能 - 当设备处于水平位置时。 (目前 - 当您水平翻转按钮时,按钮会消失 - 这是不受欢迎的。)

当前 XML 状态:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >



<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:background="@drawable/apn_app_go_button" />

<ImageView
android:id="@+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="@drawable/apn_app_logo" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/emblem"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/start_text"
android:textColor="#000000"
android:textSize="14sp" />



</RelativeLayout>

问题是当我尝试将上面的 XML 更改为 ScrollView 时:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >



<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:background="@drawable/apn_app_go_button" />

<ImageView
android:id="@+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="@drawable/apn_app_logo" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/emblem"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/start_text"
android:textColor="#000000"
android:textSize="14sp" />



</ScrollView>

我遇到致命异常:

08-23 11:06:52.419: E/AndroidRuntime(3919): FATAL EXCEPTION: main
08-23 11:06:52.419: E/AndroidRuntime(3919): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.new.datasettings/com.project.new.datasettings.StartActivity}: java.lang.IllegalStateException: ScrollView can host only one direct child
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.os.Looper.loop(Looper.java:137)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.ActivityThread.main(ActivityThread.java:4429)
08-23 11:06:52.419: E/AndroidRuntime(3919): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 11:06:52.419: E/AndroidRuntime(3919): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 11:06:52.419: E/AndroidRuntime(3919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-23 11:06:52.419: E/AndroidRuntime(3919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-23 11:06:52.419: E/AndroidRuntime(3919): at dalvik.system.NativeStart.main(Native Method)
08-23 11:06:52.419: E/AndroidRuntime(3919): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.widget.ScrollView.addView(ScrollView.java:241)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.view.LayoutInflater.rInflate(LayoutInflater.java:743)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-23 11:06:52.419: E/AndroidRuntime(3919): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:252)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.Activity.setContentView(Activity.java:1841)
08-23 11:06:52.419: E/AndroidRuntime(3919): at com.project.new.datasettings.StartActivity.onCreate(StartActivity.java:44)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.Activity.performCreate(Activity.java:4471)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-23 11:06:52.419: E/AndroidRuntime(3919): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
08-23 11:06:52.419: E/AndroidRuntime(3919): ... 11 more

最佳答案

一个ScrollView只能有一个子元素。您有 3 个按钮、ImageView 和 TextView。尝试用 ScrollView 包装您的 RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >



<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:background="@drawable/apn_app_go_button" />

<ImageView
android:id="@+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="@drawable/apn_app_logo" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/emblem"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/start_text"
android:textColor="#000000"
android:textSize="14sp" />



</RelativeLayout>
</ScrollView>

关于java - 实现 ScrollView 导致致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18406339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com