Android布局崩溃: Binary XML file line #10 must supply a layout width

标签 android exception layout resources crash

我被难住了,我已经推断它位于较大布局的合并部分中(编辑谢谢,是的,第 10 行有点明显,第 10 行的 TextView 很明显需要宽度和高度,但是仅使用代码中进一步存在的 TextView (我已将其粘贴到第 #10 行)就不会发生崩溃)

<merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/frameLayoutLatest">
<LinearLayout android:id="@+id/subLinLayoutHeader" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
    <ImageView android:src="@drawable/layouttriangle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"></ImageView>
</LinearLayout>
<TableLayout android:id="@+id/subLinLayout" android:layout_below="@id/subLinLayoutHeader" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    <TableRow  android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
            <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
                <!-- list view goes here -->
                <TextView android:layout_gravity="center_vertical|center_horizontal" android:text="Dummy text" android:textColor="#ffffff" android:textSize="16dip"></TextView>
            </ScrollView>
        </LinearLayout>
    </TableRow>
    <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
            <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
                <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content">
                <TextView android:layout_gravity="center_vertical|center_horizontal" android:text="Latest Articles" android:textColor="#ffffff" android:textSize="16dip"></TextView>
                </TableRow>
        </TableLayout>
        <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
         <!--  list view goes here -->
        </ScrollView>
        </LinearLayout>

</TableRow>
</TableLayout>

沃尔多在哪里?

最佳答案

当然是在第 10 行:) 您的代码不提供 TextView 的layout_height 或layout_width。每个 xml 元素都需要两者。

这是您的代码:

<TextView
android:layout_gravity="center_vertical|center_horizontal"
android:text="Dummy text"
android:textColor="#ffffff"
android:textSize="16dip">
</TextView>

试试这个(或者使用match_parent而不是wrap_content)

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Dummy text"
android:textColor="#ffffff"
android:textSize="16dip">
</TextView>

编辑以解释第二个 TextView 的行为

嗯,今天我学到了一些新东西。 根据 TableRow 文档,

The children of a TableRow do not need to specify the layout_width
and layout_height attributes in the XML file. TableRow always enforces
those values to be respectively MATCH_PARENT and WRAP_CONTENT.

这就是它没有抛出错误的原因。

关于Android布局崩溃: Binary XML file line #10 must supply a layout width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6682475/

相关文章:

android - 如何只删除横向特定的 xml 布局文件?

键盘 View 中的 Android 布局

java - 面试问题 - 如何处理基于 SOAP 和基于 REST 的异常

java - 当我们显式设置容器的位置时, pack() 函数如何表现?

html - 使用@font-face 添加新字体不起作用

c# - 登录前隐藏菜单,登录后显示

android - 尝试将项目从 bitbucket 导入 android studio 时遇到问题 "Repository test has failure"

java - 在 android 中使用微调器过滤 Listview

java - 如何正确管理 EJBException

java - 错误消息 Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException :