Cómo agregar TableRow de xml a TableLayout mediante programación?

Im nuevo a Android y estoy tratando de agregar un TableRow, que ya he hecho con xml, a un TableLayout por programación. Estoy recibiendo Force Closes, la mayoría son NullPointerExcpetions.

Aquí está mi clase de java

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; } } 

Este es mi diseño con el TableLayout:

 <?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> 

Y el 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> 

He intentado muchas maneras diferentes de lograrlo pero todavía nada.

Reemplazar código:

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

con

 View tr = inflater.inflate(R.layout.table_row_layout, null,false); tl.addView(tr); //not working, obviously im missing something 
  • ¿Cómo obtengo un botón para abrir otra actividad en Android Studio?
  • Cambiar el color de Switch en Android
  • Las comillas no se muestran en la pantalla
  • InflateException: Línea de archivo XML binario # 1: Error de inflar clase <unknown> causada por OutOfMemoryError
  • Androide. OnEditorAction nunca llamado
  • Diseño gráfico de eclipse que no muestra nada
  • Swich imágenes de fondo se extiende al personalizar en android
  • Java.lang.IllegalStateException Necesita utilizar un tema Theme.AppCompat (o descendente) con esta actividad
  • Androide. Cambiar el color de fondo de un FrameLayout de código
  • ¿Cómo puedo añadir una línea blanca 1px horizontal debajo del botón en un diseño relativo?
  • Fuera de error de memoria al inflar el diseño xml simple en android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.