动态地向表布局添加多行

时间:2012-10-23 19:47:20

标签: android tablelayout android-tablelayout

在没有运气的情况下搜索了一段时间后我决定问这个......

我有一个SQLite数据库,其中包含我想要在GUI中显示的信息,但我没有使用ListView,我正在使用TableLayout ......所以我在互联网上找到了这个代码:

/* Find Tablelayout defined in main.xml */
      TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
           /* Create a new row to be added. */
           TableRow tr = new TableRow(this);
           tr.setLayoutParams(new LayoutParams(
                          LayoutParams.FILL_PARENT,
                          LayoutParams.WRAP_CONTENT));
                /* Create a Button to be the row-content. */
                Button b = new Button(this);
                b.setText("Dynamic Button");
                b.setLayoutParams(new LayoutParams(
                          LayoutParams.FILL_PARENT,
                          LayoutParams.WRAP_CONTENT));
                /* Add Button to row. */
                tr.addView(b);
      /* Add row to TableLayout. */
      tl.addView(tr,new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

我想修改它以便我可以在for循环中执行此操作,但在将其添加到TableLayout之前还要向TablaRow插入三个TextView。目前,我有这个代码:

//Global Variables
TableLayout tabla = (TableLayout) findViewById(R.id.tabla1);
public static DataBaseHelper mDbH;
String rutas[][];
int COLUMNAS;
int FILAS;

//onCreate method
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rutas);
    mDbH = new DataBaseHelper(this);


    obtenerDatosRutas();

    //tabla.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    for(int i=0;i<FILAS;i++){
        addFila(rutas[i][COLUMNAS-3],rutas[i][COLUMNAS-2],rutas[i][COLUMNAS-1]);
    }

}

//obtenerDatosRutas method outside onCreate method
public void obtenerDatosRutas(){
    mDbH.open();
    Cursor c = null;
    c = mDbH.consulta1();
    startManagingCursor(c);
    COLUMNAS = c.getColumnCount();
    FILAS = c.getCount();
    rutas = new String[FILAS][COLUMNAS];
    if(c.moveToFirst() == false){
        Log.e("cursor c ","vacio");
        c.close();
        mDbH.close();
    }else{
        for(int i=0;i<FILAS;i++){
            for(int j=0;j<COLUMNAS;j++){
                rutas[i][j] = c.getString(j);
            }
            c.moveToNext();
        }
        c.close();
        mDbH.close();
    }
}

 //addFila method outside onCreate method
 private void addFila(String string, String string2, String string3) {
    // TODO Auto-generated method stub
    TableRow row = new TableRow(this);
    row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    TextView tv1 = new TextView(this);
    tv1.setText(string);
    tv1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    row.addView(tv1);
    TextView tv2 = new TextView(this);
    tv2.setText(string2);
    tv2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    row.addView(tv2);
    TextView tv3 = new TextView(this);
    tv3.setText(string3);
    tv3.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    row.addView(tv3);
    tabla.addView(row,new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
}

这是logcat显示的错误...它表示是由于TableLayout tabla =(TableLayout)findViewById(R.id.tabla1);

行中的NullPointerException
10-23 14:50:55.062: E/AndroidRuntime(2738): FATAL EXCEPTION: main
10-23 14:50:55.062: E/AndroidRuntime(2738): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tian.proto/tian.proto.pantalla3}: java.lang.NullPointerException
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.os.Looper.loop(Looper.java:130)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at java.lang.reflect.Method.invoke(Method.java:507)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at dalvik.system.NativeStart.main(Native Method)
10-23 14:50:55.062: E/AndroidRuntime(2738): Caused by: java.lang.NullPointerException
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.app.Activity.findViewById(Activity.java:1647)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at tian.proto.pantalla3.<init>(pantalla3.java:21)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at java.lang.Class.newInstanceImpl(Native Method)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at java.lang.Class.newInstance(Class.java:1409)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-23 14:50:55.062: E/AndroidRuntime(2738):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
10-23 14:50:55.062: E/AndroidRuntime(2738):     ... 11 more

我不知道为什么,因为我在我的XML文件中声明了id(tabla1):

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabla1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#006ca5"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
> ...   </TableLayout>

任何线索?提前谢谢!

1 个答案:

答案 0 :(得分:0)

//Global Variables 
TableLayout tabla = (TableLayout) findViewById(R.id.tabla1); 

^这是你的问题。

findViewById()在当前内容视图中搜索包含您传入的ID的视图。但是,在setContentView(R.layout.rutas)生命周期事件之前,您不会设置内容视图(onCreate())。您可以将TableLayout声明为全局,但在致电findViewById()之前,您无法使用setContentView()进行分配。