0

ウィジェット/ビューを動的に作成するコードを再配置しました。xmlレイアウトは「整形式」(コンパイル)のように見えますが、対応する「setContentView()」に到達するとクラッシュします。

IOW:

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を追加したのに、なぜ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」に余分な直角ブラケットが含まれていることがわかりました。そこで、最初の1つを取り出したところ、エラーメッセージが表示されました。つまり、「指定された名前(「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

上記で投稿した XML には、注意すべき点がいくつかあります。

1-最初のテーブル行を使用している場合

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

この行では、行の layout_height および layout_width 属性を指定していないため、警告で「役に立たない」ことになります。

2-もう1つのポイントは、ボタンのスクロールビューでテーブルレイアウトにidを次のように指定してみてください。

 android:id="@+id/tlDynamicRows"

それ以外の

 android:id="@id/tlDynamicRows"

これらがうまくいくことを願っています。

于 2012-03-14T05:23:42.397 に答える
1

これは、TableRow が 1 つしかないために発生している可能性があります。さらに行を追加して、これが持続するかどうかを確認してください。

参考文献:

于 2012-03-14T05:16:54.877 に答える