Question

Je suis débutant dans la programmation Android, donc j'ai un problème avec l'ajout de lignes à TableLayout dynamiquement. S'il vous plaît, voir un code ci-dessous:

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}

Qu'est-ce que je fais mal?

Était-ce utile?

La solution

Vous essayez d'obtenir la mise en page avec mauvais identifiant. Supposons que vous avez le main.xml suivant:

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

Ainsi, dans le code que vous devez écrire:

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

En utilisant le même identifiant pour la mise en page et afficher le contenu est erroné. Le fait est que la recherche findViewById pour l'id, passé en paramètre, dans le xml gonflé, vous avez passé à setContentView. Mais il n'y a pas R.layout.main dans main.xml, mais il est là R.id.new_activity_layout. Hope, l'explication est assez claire.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top