0

我重新安排了我的代碼,它動態地創建窗口小部件/視圖,儘管xml佈局看起來是「格式良好」(它編譯),當我到達相應的「setContentView()」時它崩潰了。爲什麼編譯器說我沒有給我的TableLayout一個ID?

督察:

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> 

什麼可能導致這種反叛的發展?

更新時間:

當找過我的帖子,我看到最後的「TableLayout」在XML中有一個多餘的右尖括號。所以,我把第一個出來了,現在我得到了一個錯誤信息,即「找不到與給定名稱匹配的資源(在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> 

我仍然有一個警告信息來改變這條線在TableLayout了滾動內:

android:layout_height="wrap_content" 

到:

android:layout_height="wrap_content" 

我做了,似乎沒有什麼區別 - 但它擺脫了警告,所以我會離開它。

回答

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" 

希望你們這些工作。

+0

我看到的所有例子都有「+」 刪除所有這些例子現在給出了三個元素中的錯誤信息,而不僅僅是一個,例如,這樣的: 機器人:ID = 「@ ID/scrollViewHost」 給出了這樣的: 錯誤:錯誤:沒有資源發現在給定的名稱(與價值 '@ ID/scrollViewHost' 的 '身份證')相匹配。 ... @ id/tlDynamic和@ id/tlDynamicRows同樣如此。 所以我加回了「+」,在包含TableRow的layout_width和layout_height屬性後,它終於起作用了。我已經爲後人添加了我的新xml。 – 2012-03-14 14:09:00

+0

我仍然得到,「這TableRor佈局或TableLayout父母是沒用的」,但它正在做我想要它做的... – 2012-03-14 14:11:15

+0

@ClayShannon ..這個鏈接可能會幫助你理解表格佈局......看看。 ..http://developer.android.com/reference/android/widget/TableLayout.html – 2012-03-15 04:56:08

相關問題