title: Problem with adding rows in TableLayout programmatically
body: <p>I am trying to add rows in a <code>TableLayout</code> programmatically and I am following instructions given in following links:</p>
  
  <p><a href="http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout" rel="nofollow">Dynamically Adding Rows to TableLayout</a> and <a href="http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside-a-tablelayout-programatically/" rel="nofollow">Creating Table Rows Inside A Table Layout Programmatically</a></p>
  
  <p>I am always getting <code>Application has stopped unexpectedly. Please try again.</code> error. If I define the whole <code>TableLayout</code> in XML, then it works fine but when I try to do it programmatically, I always get the that error.</p>
  
  <p>Here is the code of my XML file (main.xml):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:stretchColumns="1"
      android:id="@+id/maintable"&gt;
      &lt;TableRow
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;
          &lt;TextView
              android:layout_column="1"
              android:text="Open..."
              android:padding="3dip" /&gt;
          &lt;TextView
              android:text="Ctrl-O"
              android:gravity="right"
              android:padding="3dip" /&gt;
      &lt;/TableRow&gt;
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>and this is the code I've done in my .java:</p>
  
  <pre><code>public class HelloTableLayout extends Activity {
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          /* Find Tablelayout defined in main.xml */
          TableLayout tl = (TableLayout)findViewById(R.id.maintable);
          /* 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));
      }
  }
  </code></pre>
  
  <p>Here is LogCat for the problem:</p>
  
  <pre><code>04-04 13:56:21.161: DEBUG/AndroidRuntime(502): &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; AndroidRuntime START &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  04-04 13:56:21.171: DEBUG/AndroidRuntime(502): CheckJNI is ON
  04-04 13:56:21.441: DEBUG/AndroidRuntime(502): --- registering native functions ---
  04-04 13:56:21.871: DEBUG/ddm-heap(502): Got feature list request
  04-04 13:56:22.411: DEBUG/AndroidRuntime(502): Shutting down VM
  04-04 13:56:22.411: DEBUG/dalvikvm(502): DestroyJavaVM waiting for non-daemon threads to exit
  04-04 13:56:22.411: DEBUG/dalvikvm(502): DestroyJavaVM shutting VM down
  04-04 13:56:22.421: DEBUG/dalvikvm(502): HeapWorker thread shutting down
  04-04 13:56:22.421: DEBUG/dalvikvm(502): HeapWorker thread has shut down
  04-04 13:56:22.431: DEBUG/jdwp(502): JDWP shutting down net...
  04-04 13:56:22.431: ERROR/AndroidRuntime(502): ERROR: thread attach failed
  04-04 13:56:22.441: INFO/dalvikvm(502): Debugger has detached; object registry had 1 entries
  04-04 13:56:22.451: DEBUG/dalvikvm(502): VM cleaning up
  04-04 13:56:22.481: DEBUG/dalvikvm(502): LinearAlloc 0x0 used 629532 of 5242880 (12%)
  04-04 13:56:23.111: DEBUG/AndroidRuntime(510): &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; AndroidRuntime START &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  04-04 13:56:23.121: DEBUG/AndroidRuntime(510): CheckJNI is ON
  04-04 13:56:23.391: DEBUG/AndroidRuntime(510): --- registering native functions ---
  04-04 13:56:23.821: DEBUG/ddm-heap(510): Got feature list request
  04-04 13:56:24.401: INFO/ActivityManager(35): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.vision.HelloLinearLayout/.HelloLinearLayout }
  04-04 13:56:24.471: DEBUG/AndroidRuntime(510): Shutting down VM
  04-04 13:56:24.491: DEBUG/dalvikvm(510): DestroyJavaVM waiting for non-daemon threads to exit
  04-04 13:56:24.521: DEBUG/dalvikvm(510): DestroyJavaVM shutting VM down
  04-04 13:56:24.521: DEBUG/dalvikvm(510): HeapWorker thread shutting down
  04-04 13:56:24.547: ERROR/AndroidRuntime(510): ERROR: thread attach failed
  04-04 13:56:24.571: DEBUG/dalvikvm(510): HeapWorker thread has shut down
  04-04 13:56:24.591: DEBUG/jdwp(510): JDWP shutting down net...
  04-04 13:56:24.611: INFO/dalvikvm(510): Debugger has detached; object registry had 1 entries
  04-04 13:56:24.611: DEBUG/dalvikvm(510): VM cleaning up
  04-04 13:56:24.761: INFO/ActivityManager(35): Start proc com.vision.HelloLinearLayout for activity com.vision.HelloLinearLayout/.HelloLinearLayout: pid=517 uid=10028 gids={3003}
  04-04 13:56:24.771: DEBUG/dalvikvm(510): LinearAlloc 0x0 used 639500 of 5242880 (12%)
  04-04 13:56:25.041: DEBUG/ddm-heap(517): Got feature list request
  04-04 13:56:25.651: DEBUG/AndroidRuntime(517): Shutting down VM
  04-04 13:56:25.661: WARN/dalvikvm(517): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
  04-04 13:56:25.661: ERROR/AndroidRuntime(517): Uncaught handler: thread main exiting due to uncaught exception
  04-04 13:56:25.692: ERROR/AndroidRuntime(517): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vision.HelloLinearLayout/com.vision.HelloLinearLayout.HelloLinearLayout}: java.lang.NullPointerException
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.os.Handler.dispatchMessage(Handler.java:99)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.os.Looper.loop(Looper.java:123)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.main(ActivityThread.java:4363)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at java.lang.reflect.Method.invokeNative(Native Method)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at java.lang.reflect.Method.invoke(Method.java:521)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at dalvik.system.NativeStart.main(Native Method)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517): Caused by: java.lang.NullPointerException
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at com.vision.HelloLinearLayout.HelloLinearLayout.onCreate(HelloLinearLayout.java:35)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
  04-04 13:56:25.692: ERROR/AndroidRuntime(517):     ... 11 more
  04-04 13:56:25.731: INFO/Process(35): Sending signal. PID: 517 SIG: 3
  04-04 13:56:25.741: INFO/dalvikvm(517): threadid=7: reacting to signal 3
  04-04 13:56:25.741: INFO/dalvikvm(517): Wrote stack trace to '/data/anr/traces.txt'
  04-04 13:56:34.527: WARN/ActivityManager(35): Launch timeout has expired, giving up wake lock!
  04-04 13:56:35.244: WARN/ActivityManager(35): Activity idle timeout for HistoryRecord{43cb3358 com.vision.HelloLinearLayout/.HelloLinearLayout}
  04-04 13:56:40.471: DEBUG/dalvikvm(98): GC freed 184 objects / 7888 bytes in 185ms
  04-04 13:56:42.241: INFO/Process(517): Sending signal. PID: 517 SIG: 9
  04-04 13:56:42.281: INFO/ActivityManager(35): Process com.vision.HelloLinearLayout (pid 517) has died.
  04-04 13:56:42.361: INFO/UsageStats(35): Unexpected resume of com.android.launcher while already resumed in com.vision.HelloLinearLayout
  04-04 13:56:42.461: ERROR/gralloc(35): [unregister] handle 0x342e08 still locked (state=40000001)
  04-04 13:56:42.471: WARN/InputManagerService(35): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43cd6e30
  04-04 13:57:04.991: DEBUG/dalvikvm(94): GC freed 13267 objects / 589160 bytes in 124ms
  04-04 14:14:44.061: DEBUG/dalvikvm(35): threadid=15: bogus mon 1+0&gt;0; adjusting
  </code></pre>
  
  <p>Please help me sorting out this issue and guide me what I am doing wrong. </p>