2013-01-03 13 views
9

私はAndroidには新しく、既にxmlで作成したTableRowをTableLayoutにプログラムで追加しようとしています。私はForce Closesを取得しています。ほとんどがNullPointerExcpetionsです。xmlからTableRowをTableLayoutにプログラムで追加するには?

ここに私のJavaクラスはこれがTableLayoutと私のレイアウトである

public class DayFragment extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.subjects_list, container, false); 


    TableLayout tl = (TableLayout) view.findViewById(R.id.tl); 
    TableRow tr = (TableRow) view.findViewById(R.id.tr_row); 

      tl.addView(tr); //not working, obviously im missing something 

    //xml parsing stuff 

      return view; 
} 
} 

です:

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

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:orientation="vertical" > 

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

     <include 
      android:id="@+id/header" 
      layout="@layout/table_header" /> 

     <include android:id="@+id/h_divider" 
      layout="@layout/horizontal_divider"/> 




    </TableLayout> 
</ScrollView> 

</HorizontalScrollView> 

とのTableRow:

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tr_row" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" > 

<TextView 
    android:id="@+id/tv_hour" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:padding="8dp" 
    android:textSize="15sp" 
    android:textStyle="bold" /> 

<View 
    android:layout_weight="1" 
    android:background="@drawable/vertical_cell_divider" /> 

<TextView 
    android:id="@+id/tv_subject" 
    android:layout_weight="1" 
    android:gravity="left" 
    android:padding="8dp" 
    android:textSize="18sp" /> 

<View 
    android:layout_weight="1" 
    android:background="@drawable/vertical_cell_divider" /> 

<TextView 
    android:id="@+id/tv_start" 
    android:layout_weight="1" 
    android:gravity="left" 
    android:padding="8dp" 
    android:textSize="18sp" /> 

<TextView 
    android:id="@+id/tv_bar" 
    android:layout_weight="1" 
    android:gravity="left" 
    android:textSize="18sp" /> 

<TextView 
    android:id="@+id/tv_end" 
    android:layout_weight="1" 
    android:gravity="left" 
    android:padding="8dp" 
    android:textSize="18sp" /> 

</TableRow> 

私はそれを達成するためのさまざまな方法の多くを試してみましたまだ何もない。

答えて

9

コードを置き換えます。

View tr = inflater.inflate(R.layout.table_row_layout, null,false); 
tl.addView(tr); //not working, obviously im missing something 
+0

TableRow tr = (TableRow) view.findViewById(R.id.tr_row); tl.addView(tr); //not working, obviously im missing something 

をYES!私の問題を解決してくれてありがとう。同じビューを複数回追加する方法も教えてください。子はすでに親を持っているので、tl.addView(tr)を置くだけでもっと追加することはできません。 – Chris95X8

+0

ループ内のコードを繰り返します。 forループを繰り返す前に、ビュー配列を作成しておいてください。配列内のすべてのビューに対して、table_row_layoutを展開してTable Layoutに追加します。 – TNR

+0

私はちょうどこのようにそれをやったし、それは、(i = 0をint型;私は7を<;私は++)のため を働い{ \t \t \t \t \tビューTR = inflater.inflate(ヌルR.layout.subject_row、偽) ; \t \t tl.addView(tr); \t \t \t \t} – Chris95X8

関連する問題