当前位置: 首页 > 知识库问答 >
问题:

我的应用程序已成功构建,但不断崩溃

易扬
2023-03-14

我有一个制作android应用程序的学校项目。但当我运行应用程序时,它会在logcat中不断显示类似的内容。我是android studio的新手。请帮帮我。

2020-04-03 00:10:26.352 31907-31907/com.example.citypage E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.citypage, PID: 31907
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.citypage/com.example.citypage.MainActivity}: android.view.InflateException: Binary XML file line #88 in com.example.citypage:layout/activity_main: ScrollView can host only one direct child
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3448)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7814)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
     Caused by: android.view.InflateException: Binary XML file line #88 in com.example.citypage:layout/activity_main: ScrollView can host only one direct child
     Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
        at android.widget.ScrollView.addView(ScrollView.java:599)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.citypage.MainActivity.onCreate(MainActivity.java:13)
        at android.app.Activity.performCreate(Activity.java:7955)
        at android.app.Activity.performCreate(Activity.java:7944)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7814)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
2020-04-03 00:10:26.377 31907-31907/com.example.citypage I/Process: Sending signal. PID: 31907 SIG: 9

共有3个答案

楚宏胜
2023-03-14

ScrollView中只保留一个Layout。因为它只能托管一个直接子级。类似于下面的代码。它不是强制使用ConstraintLayout,任何布局都可以工作。但ScrollView中只有一个直接子级

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        ----- //child Layout 1

        ----- //child layout 2

     </androidx.constraintlayout.widget.ConstraintLayout>

 </ScrollView>
融伯寅
2023-03-14

您的错误是“ScrollView只能承载一个直接子级”。通过将所有其他视图包装到LinearLayout或其他布局视图中,确保ScrollView中只有一个视图。

梁丘柏
2023-03-14

检查您的:布局/activity_main文件中的ScrollView,您可能在ScrollView中有多个子,删除它们或合并它们。

在ScrollView中,不能放置多个直接子级。

 类似资料: