3

Here i have referenced code link

I have to add multiple textviews @ runtime and maybe its working but textviews are not shown and I have to add table row in table layout and show textview. But its not working.

Here is my code :

tx[i] = new TextView(S1.this);

                    tx[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                    tx[i].setText(name);

                    tr[i] = new TableRow(S1.this);
                    tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                    tr[i].addView(tx[i]);
                    rl.addView(tr[i]); // Exception

Here exception is thrown and app stops working.

Here is my logcat :

04-08 02:13:05.255: E/AndroidRuntime(1716): FATAL EXCEPTION: AsyncTask #1
04-08 02:13:05.255: E/AndroidRuntime(1716): Process: com.app.crossdine, PID: 1716
04-08 02:13:05.255: E/AndroidRuntime(1716): java.lang.RuntimeException: An error occured while executing doInBackground()
04-08 02:13:05.255: E/AndroidRuntime(1716):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at java.lang.Thread.run(Thread.java:841)
04-08 02:13:05.255: E/AndroidRuntime(1716): Caused by: java.lang.NullPointerException
04-08 02:13:05.255: E/AndroidRuntime(1716):     at com.app.crossdine.S1$Getitems.doInBackground(S1.java:155)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at com.app.crossdine.S1$Getitems.doInBackground(S1.java:1)
04-08 02:13:05.255: E/AndroidRuntime(1716):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
 04-08 02:13:05.255: E/AndroidRuntime(1716):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

Please guide me.

kittu
  • 43
  • 6

1 Answers1

1

With the help of the link provided in the question and also the following link, you can use it like below (make sure the rl is properly pointing to the layout/view you are adding this table to).

UPDATE: Because you are using AsyncTask, you can not modify the UI content in this method. I did in onPostExecute method. Also, you should set the layout parameters of the TextView with TableRow.LayoutParams. My working code below

First added the following to a button in my xml layout

android:onClick="addTable" 

Then added the following the the java activity

public void addTable(View view){
    CreateTableTask mTask = new CreateTableTask();
    mTask.execute();
}

public class CreateTableTask extends AsyncTask<Void, Void, TableLayout> {
@Override
protected TableLayout doInBackground(Void... params) {
    LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
    TableLayout layoutINNER = new TableLayout(MainActivity.this);
    layoutINNER.setLayoutParams(params1);
    TextView[] tx = new TextView[10];
    TableRow[] tr = new TableRow[10];
    for(int i=0; i<10; i++) {
        tx[i] = new TextView(MainActivity.this);
        tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    tx[i].setText("Data");
    tr[i] = new TableRow(MainActivity.this);
    tr[i].setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    tr[i].addView(tx[i]);
    // and then adding table row in tablelayout
    layoutINNER.addView(tr[i]);
    }
    return layoutINNER;
}

@Override
protected void onPostExecute(final TableLayout newTable) {
    if (newTable!=null) {
            // this rl is the layout where I want this table
        View rl= findViewById(R.id.mainlayout);
        ((ViewGroup) rl).addView(newTable);             
    } 
}       
}    
Community
  • 1
  • 1
Tau
  • 468
  • 3
  • 13
  • when i am writing following line layoutINNER.addView(tr[i]); it gives me same exception as above on that line – kittu Apr 08 '14 at 07:12
  • @kittu, are you adding this view in the doInBackground method of your asynctask? – Tau Apr 08 '14 at 07:32
  • I implemented the same thing in asynctask without problem. See the updated code and see if you can make anything out of it. @kittu – Tau Apr 08 '14 at 10:57
  • I cant find r.id.RelativeLayout ? What should i do ?@user1226136 – kittu Apr 08 '14 at 11:02
  • at xml layout make sure you defined the id tag (e.g. in my case I added the following attribute to the linear layout of my choice `android:id="@+id/mainlayout"` ) and then you should be able find the id in your code. – Tau Apr 08 '14 at 11:06