Pregunta

Soy novato en la programación de Android, así que tengo un problema con la adición de filas a TableLayout dinámicamente. Por favor, consulte a un código de abajo:

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

registro:

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}

Lo que hace mal?

¿Fue útil?

Solución

Se trata de conseguir el diseño con id mal. Suponga que tiene la siguiente 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"
            >

Por lo tanto, en el código que debe escribir:

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

utilizando el mismo ID para el diseño y ver el contenido es erróneo. El hecho es que findViewById buscar el ID, aprobada en el parámetro, dentro del xml inflado, que pasó a setContentView. Pero no hay R.layout.main en main.xml, pero hay R.id.new_activity_layout allí. Esperanza, la explicación es bastante clara.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top