"ScrollView can host only one direct child", scrollviewdirect
1. Problem description:
(Note the following black characters ).
An error is reported when ScrollView is used as the top-level view, which directly causes the apk to crash. The detailed error message is as follows:
12-21 09:12:15. 150: D/AndroidRuntime (7726): Shutting down VM
12-21 09:12:15. 150: W/dalvikvm (7726): threadid = 1: thread exiting with uncaught exception (group = 0x416402a0)
12-21 09:12:15. 160: E/AndroidRuntime (7726): fatal exception: main
12-21 09:12:15. 160: E/AndroidRuntime (7726): java. lang. RuntimeException:Unable to start activity ComponentInfo {com. project. fish. app/com. my. act. activity. my. MyFA}: java. lang. IllegalStateException: ScrollView can host only one direct child
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. ActivityThread. initialize mlaunchactivity (ActivityThread. java: 2110)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. ActivityThread. handleLaunchActivity (ActivityThread. java: 2135)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. ActivityThread. access $700 (ActivityThread. java: 140)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. ActivityThread $ H. handleMessage (ActivityThread. java: 1237)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. OS. Handler. dispatchMessage (Handler. java: 99)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. OS. Looper. loop (Looper. java: 137)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. ActivityThread. main (ActivityThread. java: 4921)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at java. lang. reflect. Method. invokeNative (Native Method)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at java. lang. reflect. Method. invoke (Method. java: 511)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at com. android. internal. OS. ZygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 1006)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at com. android. internal. OS. ZygoteInit. main (ZygoteInit. java: 773)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at dalvik. system. NativeStart. main (Native Method)
12-21 09:12:15. 160: E/AndroidRuntime (7726): Caused by: java. lang. IllegalStateException:ScrollView can host only one direct child
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. widget. ScrollView. addView (ScrollView. java: 316)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. view. LayoutInflater. rInflate (LayoutInflater. java: 750)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. view. LayoutInflater. inflate (LayoutInflater. java: 489)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. view. LayoutInflater. inflate (LayoutInflater. java: 396)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. view. LayoutInflater. inflate (LayoutInflater. java: 352)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at com. android. internal. policy. impl. PhoneWindow. setContentView (PhoneWindow. java: 307)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. Activity. setContentView (Activity. java: 1924)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at com. my. act. InitTitlebar (MyFA. java: 113)
12-21 09:12:15. 160: E/AndroidRuntime (7726 ):At com. ui. activity. base. BaseFA. onCreate (BaseFA. java: 29)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at com. fish. ui. activity. my. MyFA. onCreate (MyFA. java: 169)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. Activity. Wait mcreate (Activity. java: 5206)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. Instrumentation. callActivityOnCreate (Instrumentation. java: 1094)
12-21 09:12:15. 160: E/AndroidRuntime (7726): at android. app. ActivityThread. initialize mlaunchactivity (ActivityThread. java: 2074)
12-21 09:12:15. 160: E/AndroidRuntime (7726):... 11 more
2. Analysis:
Slave error messageScrollView can host only one direct child
As you can see, it is an xml error. The following lists the error xml files:
<? Xml version = "1.0" encoding = "UTF-8"?>
<ScrollView xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "@ color/WhiteColor"
Android: scrollbars = "vertical">
<! -- =========================== 1. image ========================================= -->
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "horizontal"
Android: paddingTop = "15dip">
<ImageView
Android: id = "@ + id/imageView_pic"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: contentDescription = "text"
Android: scaleType = "fitCenter"
Android: src = "@ drawable/empty_photo"/>
</LinearLayout>
<! -- =====================================2. detail ========================== -->
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "32dp"
Android: layout_marginTop = "10dp"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/text_prompt"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: layout_marginLeft = "5dp"
Android: gravity = "center"
Android: text = "@ string/text_detail"
Android: textSize = "15dip"/>
</LinearLayout>
<! -- =====================================3. line ======================== -->
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "32dp"
Android: layout_marginTop = "4dp"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "2dp"
Android: background = "@ color/lightgray"/>
</LinearLayout>
</ScrollView>
, This ScrollView contains three LinerLayout.
3. solution:
From the error message, we know that scrollview cannot have multiple children. Therefore, I added a layout in addition to three layout views, make the three layou as the child views of the newly added layout and solve the problem. The following are the correct xml definitions:
<? Xml version = "1.0" encoding = "UTF-8"?>
<ScrollView xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "@ color/WhiteColor"
Android: scrollbars = "vertical">
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
Android: paddingTop = "15dip">
<! -- =========================== 1. image ========================================= -->
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "horizontal"
Android: paddingTop = "15dip">
<ImageView
Android: id = "@ + id/imageView_pic"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: contentDescription = "text"
Android: scaleType = "fitCenter"
Android: src = "@ drawable/empty_photo"/>
</LinearLayout>
<! -- =====================================2. detail ========================== -->
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "32dp"
Android: layout_marginTop = "10dp"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/text_prompt"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: layout_marginLeft = "5dp"
Android: gravity = "center"
Android: text = "@ string/text_detail"
Android: textSize = "15dip"/>
</LinearLayout>
<! -- =====================================3. line ======================== -->
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "32dp"
Android: layout_marginTop = "4dp"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "2dp"
Android: background = "@ color/lightgray"/>
</LinearLayout>
</LinearLayout>
</ScrollView>