2011-11-24 35 views
0

我創建了一個表佈局命名錶,我想用下面的表格與文本視圖添加表參考ID(R.id.table)我想動態創建表格佈局!幫我在這

TableLayout table = (TableLayout) findViewById(R.id.table); 

和填充

將應用程序,當我執行這墜毀..我調試,並仔細檢查代碼 只見R.id.table值返回空沒有編譯器錯誤

這是我table.xml,其佈局文件

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrol" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
    android:id="@+id/dummy" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <TableLayout 
     android:id="@+id/table" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <TableRow 
      android:id="@+id/tablerow" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 


     </TableRow> 
    </TableLayout> 
</LinearLayout> 

是我的做法是對還是錯..我是否要參考表表的行ID ??有沒有什麼辦法可以創建動態表格佈局?

我googling所有這一切,但我沒有罰款任何合適的例子爲此..任何人都可以幫助我在這?

在此先感謝

+1

您可以發佈,這可能是有用的你的XML和代碼?基本上,如果findViewById返回null,這意味着在XML文件中沒有這樣的id的視圖。編譯器不會抱怨,因爲它在語法上都是正確的。 –

回答

1

在first.xml

創建

<TableLayout 
     android:id="@+id/TableLayout01" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
</TableLayout> 

在second.xml

<?xml version="1.0" encoding="utf-8"?> 

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"> 

    <ImageView android:id="@+id/spinnerEntryContactPhoto" android:layout_gravity="center_vertical" /> 
    <ImageView android:id="@+id/imageView2" 
      android:layout_toRightOf="@+id/spinnerEntryContactPhoto" android:layout_width="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_height="wrap_content" android:background="@drawable/dd"></ImageView> 
    <TextView android:id="@+id/spinnerEntryContactName" android:layout_gravity="center_vertical" 
    android:singleLine="true" android:layout_marginRight="10dip" /> 
    <TextView android:id="@+id/spinnerEntryContactName1" android:layout_gravity="center_vertical" /> 

</TableRow> 

在你的類

TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); 
LayoutInflater inflater = getLayoutInflater(); 
TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, table, false); 
table.addView(row); 
0

你的標題指出你動態添加表格,但你的代碼顯示你想引用一個已經存在的表?

如果當前顯示的佈局不包含該表,它將爲空。在這種情況下,您應該引用該佈局,然後調用findViewById,例如theLayoutWhichContainsTheView.findViewById(...)

如果你真的想TableLayout要使用此代碼來創建動態:

TableLayout table = new TableLayout(this); 

那麼你需要一個TableRow添加到TableLayout,然後添加ViewsTableRow

0

在你的Activity中,你有沒有設置ContentView。它應該設置爲onCreate方法如下。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.callaction); 

    TableLayout table = (TableLayout) findViewById(R.id.table);} 

請做一個乾淨的&重建項目,那麼它會重新生成R.java文件(這是根文件夾),以及。

+0

我剛纔提到的不是動態創建表格佈局的方式。因爲當我讀到你的問題時,我覺得你正在尋找一個XML文件中的Table-Layout。 – Rakhita

+0

我相信錯誤不是與R.java構建文件..如果它這樣它會顯示編譯器錯誤.. – Pradeepraj

+0

請張貼您的XML文件。這將有所幫助。 – Rakhita