ウィジェット/ビューを動的に作成するコードを再配置しました。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"
私はそうしました、そしてそれは違いがないように見えました-しかしそれは警告を取り除いたので、私はそれを残します。