0

我是 Android 编程的新手,所以在动态向 TableLayout 添加行时遇到了麻烦。请看下面的代码:

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

日志:

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}

我做错了什么?

4

1 回答 1

1

您尝试使用错误的 id 获取布局。假设您有以下 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"
            >

所以,在代码中你应该写:

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

对布局和内容视图使用相同的 id 是错误的。事实是 findViewById 搜索 id,传入参数,在膨胀的 xml 中,你传递给 setContentView。但是main.xml里面没有R.layout.main,但是里面有R.id.new_activity_layout。希望,解释很清楚。

于 2010-11-29T15:18:27.843 回答