0

我重新安排了动态创建小部件/视图的代码,尽管 xml 布局看起来“格式正确”(它可以编译),但当我到达相应的“setContentView()”时它崩溃了。

爱荷华州:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ondemandandautomatic_dynamicauthorize); <-- crashes when I F6 (Eclipse) on this line

...所以我在我的 xml 布局文件中注意到一个警告图标:“这个 TableLayout 没用(没有孩子,没有背景,没有 id)”

好吧,我要在运行时添加孩子;为什么需要背景?还有一个大问题:为什么它说它没有 id,当我添加一个时:

<TableLayout>
android:id="@id/tlDynamicRows" <-- IT'S RIGHT THERE!!!
android:layout_width="match_parent"
android:layout_height="match_parent" > 
</TableLayout>

还有另一个警告说,“这个 TableRow 或其 TableRow 父级没用”

这是整个xml:

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

    <TableLayout
        android:id="@+id/tlDynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow> <- This is where it says, "This TableRow or its TableRow parent is useless"

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/demand"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/time"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/space"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/contact"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
    </TableLayout>

    <ScrollView
        android:id="@+id/scrollViewHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <TableLayout> <- This is where it says, "says, "This TableRow or its TableRow parent is useless"
        android:id="@id/tlDynamicRows"
        android:layout_width="match_parent"
        android:layout_height="match_parent" > 
        </TableLayout>
    </ScrollView>

</LinearLayout>

是什么导致了这种令人反感的发展?

更新:

查看我的帖子时,我看到 xml 中的最终“TableLayout”有一个多余的直角括号。所以,我拿出第一个,现在我得到一个错误消息,即“找不到与给定名称匹配的资源(在'id',值为'@id/tlDynamicRows')。”

xml 的那部分现在是:

<TableLayout
    android:id="@id/tlDynamicRows"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</TableLayout>

是什么赋予了?

更新:

这是最终起作用的xml:

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

    <TableLayout
        android:id="@+id/tlDynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/demand"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/time"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/space"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/contact"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
    </TableLayout>

    <ScrollView
        android:id="@+id/scrollViewHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <TableLayout
            android:id="@+id/tlDynamicRows"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </TableLayout>
    </ScrollView>

</LinearLayout>

我仍然有一条警告消息要在 ScrollView 内的 TableLayout 中更改此行:

android:layout_height="wrap_content"

到:

android:layout_height="wrap_content"

我做到了,它似乎没有任何区别 - 但它摆脱了警告,所以我会离开它。

4

2 回答 2

1

这可能是因为您只有一个 TableRow,请尝试添加更多行以查看这种情况是否仍然存在。

参考:

于 2012-03-14T05:16:54.877 回答
1

您在上面发布的 XML 中有几点需要注意:

1-当您使用第一个表行时

  <TableRow> <- This is where it says, "This TableRow or its TableRow parent is useless"

在这一行中,您没有指定行的 layout_height 和 layout_width 属性,这就是为什么您在警告中得到“无用”的原因。

2-另一点是尝试在底部的滚动视图中为表格布局提供 id:

 android:id="@+id/tlDynamicRows"

代替

 android:id="@id/tlDynamicRows"

希望这些对你有用。

于 2012-03-14T05:23:42.397 回答