0

After search for a while without luck I decided to ask this...

I have a SQLite database with the information I want to display in the GUI, but I'm not using a ListView, I'm using a TableLayout... So I found this code right here in the internet:

/* 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));

And I want to modify it so I can do this inside a for loop, but also insert three TextView to the TablaRow before adding it to the TableLayout. Currently, I have this code:

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

And this is the error that logcat shows... it says is due to a NullPointerException in the line TableLayout tabla = (TableLayout) findViewById(R.id.tabla1);

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

And I don't know why, because I'm declaring the id (tabla1) in my XML file:

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

Any clues? Thanks in advance!

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
alois.wirkes
  • 369
  • 2
  • 7
  • 20
  • Note: if i move the TableLayout global variable and the addFila method to onCreate method, it seems to work but it only shows one row, i think the last one (because of the for loop)... – alois.wirkes Oct 23 '12 at 19:48
  • Well, thanks for the update, I wasn't aware of that... but now that look at the questions I've done... It's hard to do what you recommend me to... first, because I have only a few questions (I've been here in stakcoverflow a month, tops), and those questions have only one, two or three answers; and when those answers are not the ones you're looking for you can't check them as "accepted answer", because they are not... don't you think so? Please, don't take this personal or anything, its just an observation... – alois.wirkes Oct 23 '12 at 20:05
  • 2
    You are right, if the answer doesn't help you. Don't mark it as accepted. I was just finding this weird that on those 9 questions you have no accepted answers. Also, sometimes I don't get good answers so I edit my question or I give them feedback so they know what's wrong :) – Marc Oct 23 '12 at 20:09
  • hi[enter link description here][1] [1]: http://stackoverflow.com/questions/9542474/android-dynamically-adding-tablerow-to-tablelayout-using-an-existing-tablerow – Ravi Shinde Aug 09 '13 at 14:46
  • [ioopoipoip][1] [1]: http://stackoverflow.com/questions/9542474/android-dynamically-adding-tablerow-to-tablelayout-using-an-existing-tablerow iop;oipopipoipoipoip huiookouiouio huoo uououjou uouoo – Ravi Shinde Aug 09 '13 at 14:47

1 Answers1

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

^ There's your problem.

findViewById() searches the current content view for a view with the ID you have passed in. However, you do not set the content view (setContentView(R.layout.rutas)) until the onCreate() lifecycle event. You can declare your TableLayout as global, but you cannot assign it with findViewById() until after your call to setContentView().

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274