2009-11-04 33 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); 

    } 

當我添加另一個位圖和innerTable到outerTable和outerTable到屏幕。該應用程序提供了一個運行時jvm錯誤104 「已添加到管理器的字段已經授權時」。如何添加另一行位圖圖像

當我向outerTable中添加另一個innerTable時出現問題。不是當我將位圖添加到outerTable時。

回答

2

innerTable已被添加到outerTable,您不能再次添加它。這是因爲控件的一個實例定義了一些成員,它告訴控件的位置及其顯示方式,如父級和大小。那些成員顯然不能具有價值。所以你必須創建另一個控件,它複製第一個控件,以便它可以擁有自己的父級和大小。

在這裏,您必須創建TableLayoutManager的另一個實例(可能將其命名爲innerTable2)並將其添加到outerTable。另外,outerTable被添加了2次,這將觸發相同的錯誤,您必須創建另一個outerTable實例。

你應該寫:(注意,有更好的方法來做到這一點...)

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

請注意,您在這裏有很多重複的代碼。您可以將innerTable創建分解爲創建TableLayoutManager的函數,添加所有必需的標籤並返回新配置的TableLayoutManager。

+0

Thanksssssss。 U讓事情變得如此簡單 – Bohemian 2009-11-04 08:17:35

+0

我編輯了我的帖子以添加一些解釋。值得再讀一遍。 – 2009-11-04 08:20:41

+0

是的,這更好 – Bohemian 2009-11-04 09:19:02