"ScrollView can host only one direct child"问题解决了
1. 问题叙述性说明:
(请注意以下几点大胆)。
ScrollView作为顶层view时报错,直接导致apk崩溃。具体错误信息例如以下:
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.performLaunchActivity(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.performCreate(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.performLaunchActivity(ActivityThread.java:2074)
12-21 09:12:15.160: E/AndroidRuntime(7726): ... 11 more
2. 分析:
从错误信息ScrollView can host only one direct child
可见,是xml的错误。以下列出错误的xml文件:
<?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>
,这个ScrollView中包括3个LinerLayout。
3. 解决:
从错误信息,我们就知道,须要scrollview不能有多个child,所以,我在3个layout外,又加了一个layout。使3个layou成为这个新加的layout的子view,问题解决。
以下是正确的xml定义:
<?
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>
版权声明:本文博客原创文章,博客,未经同意,不得转载。
"ScrollView can host only one direct child"问题解决了的更多相关文章
- ScrollView can host only one direct child
Android 采用ScrollView布局时出现异常:ScrollView can host only one direct child. 异常原因: 主要是ScrollView内部只能有一个子元素 ...
- [Android] ScrollView can host only one direct child
android 采用ScrollView布局时出现异常:ScrollView can host only one direct child.主要是ScrollView内部只能有一个子元素,即不能并列两 ...
- ScrollView不能包含多个子项,ScrollView can host only one direct child
http://debuglog.iteye.com/blog/1441828 ScrollView不能包含多个子项,ScrollView can host only one direct child ...
- android 异常:ScrollView can host only one direct child
android 采用ScrollView布局时出现异常:ScrollView can host only one direct child. 主要是ScrollView内部只能有一个子元素,即不能并列 ...
- ScrollView:ScrollView can host only one direct child异常
java.lang.IllegalStateException: ScrollView can host only one direct child 原因是在外面有一个TextView控件,将其删除则 ...
- ScrollView can host only one direct child 解决
主要是ScrollView内部只能有一个子元素,即不能并列两个子元素,所以需要把所有的子元素放到一个LinearLayout内部或RelativeLayout等其他布局方式让后再在这个layout外部 ...
- CocoaPods 遇到 A host target is a "parent" target which embeds a "child" target 问题解决
正在开发的项目中,集成RN,在使用cocoapods 时候,pod install 遇到如下问题: [!] Unable to find host target(s) for ****Extensio ...
- qos-server can not bind localhost:22222, dubbo version: 2.6.0, current host: 127.0.0.1【问题解决】
好吧,这个问题比较low,但是记录一下,以免后期遗忘. 说白了,这个问题就是端口被占用了. 问题: qos-server can not bind localhost:22222, dubbo ver ...
- Java - 错误: "java.lang.ArrayIndexOutOfBoundsException: length=1; index=1"
错误: "java.lang.ArrayIndexOutOfBoundsException: length=1; index=1" 本文地址: http://blog.csdn.n ...
随机推荐
- 1.1.3-学习Opencv与MFC混合编程之---画图工具 通过对话框进行工具的参数设置 画曲线 绘图校正
源代码:http://download.csdn.net/detail/nuptboyzhb/3961688 l 对话框 1.“插入”->“资源”->“对话框” 2.对话框属性如下: 双击 ...
- MySql截取DateTime字段的日期值
用 DATE_FORMAT 来格式化日期字段 SELECT DATE_FORMAT(crt_time,'%Y-%m-%d') FROM ad_n_advertise_t
- Android中View绘制优化之三---- 优化View
本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 译三: 优化视图 关于如何设计自定义View以及响应触摸时间等,请看Android developer : 地 ...
- Boost Thread学习笔记五
多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage.boost::thr ...
- ubuntu环境ceph配置入门(一)
环境:ubuntu server 14.04 64bit,安装ceph版本号0.79 正常情况下应有多个主机,这里为了高速入门以一台主机为例,多台主机配置方式类似. 1. 配置静态IP及主机名 静态I ...
- CSS中float属性和clear属性的一些笔记
在学习CSS的最后一部分内容中,float属性和clear属性比较难以用语言描述,因此在笔记本中无法准确的记录这两个属性的用法.所以在博客园上以图文的形式记录这两种属性的特征,以备以后查阅. 首先,定 ...
- 全局忽略编译警告(设置QMAKE_CXXFLAGS )
msvc编译器从2010 sp1开始就已经支持UTF-8的源码文件了,然后到vs2012又不支持了,官方表示是BUG.到目前最新的vs2013就解决了这个问题... 但是在编译时仍然会出现4819的警 ...
- Android菜鸟的成长笔记(13)——异步任务(Async Task)
原文:[置顶] Android菜鸟的成长笔记(13)——异步任务(Async Task) Android的UI线程主要负责处理用户的事件及图形显示,因此主线程UI不能阻塞,否则会弹出一个ANR(App ...
- 利用开源HTML5引擎lufylegend.js结合javascript实现的五子棋人机对弈
前言 本文主要介绍利用开源引擎 lufylegend.js开发基于Html5的游戏--五子棋,主要叙述其详细开发过程. 游戏规则 玩过五子棋的都应该知道五子棋的规则,这里就简单介绍其规则. 1 ...
- SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签
游历SpringMVC源代码后发现,在web.xml中注冊的ContextLoaderListener监听器不过初始化了一个根上下文,只完毕了组件扫描和与容器初始化相关的一些工作,并没有探測到详细每一 ...