Question

I am newbie in Android programming, so I have a trouble with adding rows to TableLayout dynamically. Please, see a code below:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TableLayout layout = (TableLayout) findViewById(R.layout.main);
    Game game = new Game();
    Square[][] field = game.getField();
    int ii = 0;
    for (Square[] sq : field) {
        TableRow row = new TableRow(this);
        row.setId(100 + ii);
        for (Square square : sq) {
            ii++;
            Button button = new Button(this);
            String str = "button" + Integer.toString(ii);

            CharSequence text = str;
            button.setId(ii);
            button.setText(text);
            row.addView(button, new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
        }
        layout.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));
    }
}

log:

11-29 14:54:40.939: DEBUG/AndroidRuntime(337): --- registering native functions ---
11-29 14:54:41.669: INFO/ActivityManager(59): Starting activity: Intent { flg=0x10000000 cmp=com.example/.KrestikinolikiActivity }
11-29 14:54:41.809: DEBUG/AndroidRuntime(337): Shutting down VM
11-29 14:54:41.839: DEBUG/dalvikvm(337): Debugger has detached; object registry had 1 entries
11-29 14:54:41.879: INFO/ActivityManager(59): Start proc com.example for activity com.example/.KrestikinolikiActivity: pid=344 uid=10031 gids={1015}
11-29 14:54:41.919: INFO/AndroidRuntime(337): NOTE: attach of thread 'Binder Thread #3' failed
11-29 14:54:42.729: ERROR/dalvikvm(344): Could not find class 'sun.reflect.generics.reflectiveObjects.NotImplementedException', referenced from method com.example.Game.getCurrentActivePlayer
11-29 14:54:42.729: WARN/dalvikvm(344): VFY: unable to resolve new-instance 30 (Lsun/reflect/generics/reflectiveObjects/NotImplementedException;) in Lcom/example/Game;
11-29 14:54:42.739: DEBUG/dalvikvm(344): VFY: replacing opcode 0x22 at 0x0000
11-29 14:54:42.739: DEBUG/dalvikvm(344): VFY: dead code 0x0002-0005 in Lcom/example/Game;.getCurrentActivePlayer ()Lcom/example/Player;
11-29 14:54:42.759: DEBUG/AndroidRuntime(344): Shutting down VM
11-29 14:54:42.759: WARN/dalvikvm(344): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-29 14:54:42.768: ERROR/AndroidRuntime(344): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.KrestikinolikiActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.example.KrestikinolikiActivity.onCreate(KrestikinolikiActivity.java:41)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
        ... 11 more
11-29 14:54:42.789: WARN/ActivityManager(59): Force finishing activity com.example/.KrestikinolikiActivity
11-29 14:54:43.288: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{44f993c0 com.example/.KrestikinolikiActivity}

What I doing wrong?

Was it helpful?

Solution

You try to get layout with wrong id. Suppose you have the following main.xml:

<LinearLayout
            android:id="@+id/new_activity_layout"
            android:layout_alignParentTop="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

So, in code you should write:

TableLayout layout = (TableLayout) findViewById(R.id.new_activity_layout);

Using the same id for layout and content view is wrong. THe fact is that findViewById search for the id, passed in parameter, within the inflated xml, you passed to setContentView. But there is no R.layout.main in main.xml, but there is R.id.new_activity_layout there. Hope, the explanation is rather clear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top