3

I am trying to add rows in a TableLayout programmatically and I am following instructions given in following links:

Dynamically Adding Rows to TableLayout and Creating Table Rows Inside A Table Layout Programmatically

I am always getting Application has stopped unexpectedly. Please try again. error. If I define the whole TableLayout in XML, then it works fine but when I try to do it programmatically, I always get the that error.

Here is the code of my XML file (main.xml):

<?xml version="1.0" encoding="utf-8"?>
<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">
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_column="1"
            android:text="Open..."
            android:padding="3dip" />
        <TextView
            android:text="Ctrl-O"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
</TableLayout>

and this is the code I've done in my .java:

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

Here is LogCat for the problem:

04-04 13:56:21.161: DEBUG/AndroidRuntime(502): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
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): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
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>0; adjusting

Please help me sorting out this issue and guide me what I am doing wrong.

10
  • Which line of this code throws the Exception that makes your application stop? And what is the Exception? Commented Apr 4, 2011 at 8:44
  • please post logCat output! Otherwise it might be hard to find a solution! Commented Apr 4, 2011 at 8:45
  • your main.xml is a TableRow (withot the table? and where is the R.id.maintable Commented Apr 4, 2011 at 9:08
  • sorry, there was some problem posting the main.xml code, here is the complete code: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1" android:id="@+id/maintable"> </TableLayout> Commented Apr 4, 2011 at 9:18
  • how can I check that which line of code makes application stop? I mean how can I debug? Commented Apr 4, 2011 at 9:20

1 Answer 1

2

The line you want to look at is this one:

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

I'm guessing this is line 35:

tl.addView(tr,new TableLayout.LayoutParams(

And that means your call here:

 TableLayout tl = (TableLayout)findViewById(R.id.maintable);

has returned NULL?

Sign up to request clarification or add additional context in comments.

3 Comments

if you mean that there is no control with this id in my main.xml, then please review the question again. Before, it wasnt showing my main.xml code properly but I just edited my question to make it look properly. Please review the question now again. Thanks.
well, not really. That could be the issue: But the error says there is a nullpointer on line 35. If i guessed the line correctly, the only thing that could throw a nullpointer there as far as I can see, is tl. The only other line that does something with tl is the call with the findViewById. So whatever the reason, that seems to be returning NULL, unless i'm mistaken.
I dont know but for just a check, I changed the Id of the table and tried again and it worked for me. So, might be your guess was right, I guess. Was there any problem with that Id? Can anybody elaborate this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.