2009-11-04 6 views
0
class Scr extends MainScreen { 

    public Scr() { 

     TableLayoutManager outerTable = new TableLayoutManager(new int[] 
                     { 
          TableLayoutManager.USE_PREFERRED_SIZE, 
          TableLayoutManager.SPLIT_REMAINING_WIDTH 
          },0); 
     TableLayoutManager innerTable = new TableLayoutManager(new int[] 
                     { 
                     TableLayoutManager. USE_PREFERRED_SIZE, 
                     TableLayoutManager.USE_PREFERRED_SIZE 

                     }, Manager.USE_ALL_WIDTH); 

     innerTable.add(new LabelField("titleField")); 
     innerTable.add(new LabelField("title")); 
     innerTable.add(new LabelField("descriptionfield")); 
     innerTable.add(new LabelField("description")); 
     innerTable.add(new LabelField("rating field")); 
     innerTable.add(new LabelField("***")); 
     outerTable.add(new BitmapField(Bitmap.getBitmapResource("mac.png"),Field.FOCUSABLE)); 
     outerTable.add(innerTable); 
     add(outerTable); 

     outerTable.add(new BitmapField(Bitmap.getBitmapResource("fire.png"),Field.FOCUSABLE)); 
     outerTable.add(innerTable); 
     add(outerTable); 

    } 

Lorsque j'ajoute un autre bitmap et innerTable à la table externe et externalTable à l'écran. L'application donne une erreur jvm d'exécution 104 "Champ ajouté à un gestionnaire lorsqu'il est déjà parent".Comment ajouter une autre ligne de l'image bitmap

Le problème se pose lorsque j'ajoute un autre innerTable à outerTable. Pas quand j'ajoute Bitmap à outerTable.

Répondre

2

innerTable a déjà été ajouté à outerTable, vous ne pouvez pas l'ajouter une seconde fois. C'est parce qu'une instance d'un contrôle définit un membre qui indique où le contrôle et comment il affiche, comme le parent et la taille. ces membres ne peuvent manifestement pas avoir de valeur mumtiple. vous devez donc créer un autre contrôle, qui duplique le premier, afin qu'il puisse avoir son propre parent et sa taille. Ici, vous devez créer une autre instance de TableLayoutManager (peut-être nommer innerTable2) et l'ajouter à outerTable. aussi, outerTable est ajouté 2 fois, ce qui déclenchera la même erreur, vous devez créer une autre instance de outerTable.

vous devez écrire: (notez qu'il ya de meilleures façons de le faire ...)

public Scr() { 
    TableLayoutManager outerTable = new TableLayoutManager(new int[]{ 
         TableLayoutManager.USE_PREFERRED_SIZE, 
         TableLayoutManager.SPLIT_REMAINING_WIDTH 
         },0); 
    TableLayoutManager outerTable2 = new TableLayoutManager(new int[]{ 
         TableLayoutManager.USE_PREFERRED_SIZE, 
         TableLayoutManager.SPLIT_REMAINING_WIDTH 
         },0); 
    TableLayoutManager innerTable = new TableLayoutManager(new int[]{ 
         TableLayoutManager.USE_PREFERRED_SIZE, 
         TableLayoutManager.USE_PREFERRED_SIZE 
         },Manager.USE_ALL_WIDTH); 
    TableLayoutManager innerTable2 = new TableLayoutManager(new int[]{ 
         TableLayoutManager.USE_PREFERRED_SIZE, 
         TableLayoutManager.USE_PREFERRED_SIZE 
         },Manager.USE_ALL_WIDTH); 

    innerTable.add(new LabelField("titleField")); 
    innerTable.add(new LabelField("title")); 
    innerTable.add(new LabelField("descriptionfield")); 
    innerTable.add(new LabelField("description")); 
    innerTable.add(new LabelField("rating field")); 
    innerTable.add(new LabelField("***")); 
    outerTable.add(new BitmapField(Bitmap.getBitmapResource("mac.png"),Field.FOCUSABLE)); 
    outerTable.add(innerTable); 
    add(outerTable); 

    innerTable2.add(new LabelField("titleField")); 
    innerTable2.add(new LabelField("title")); 
    innerTable2.add(new LabelField("descriptionfield")); 
    innerTable2.add(new LabelField("description")); 
    innerTable2.add(new LabelField("rating field")); 
    innerTable2.add(new LabelField("***")); 
    outerTable2.add(new BitmapField(Bitmap.getBitmapResource("fire.png"),Field.FOCUSABLE)); 
    outerTable2.add(innerTable2); 
    add(outerTable2); 
} 

note que vous avez beaucoup de code en double ici. Vous pouvez factoriser la création innerTable en une fonction qui crée un TableLayoutManager, ajoute toutes les étiquettes requises et renvoie le nouveau TableLayoutManager configuré.

+0

Thanksssssss. U a fait les choses tellement facile – Bohemian

+0

j'ai édité mon poste pour ajouter quelques explications. ça vaut le coup de le lire à nouveau. –

+0

ouais c'est mieux – Bohemian

Questions connexes