2011-08-24 56 views
2

嘗試使用下面的代碼來創建表時,我得到一個「的TableView(滾動型).calculateVerticalScrollAmount(XYRect)」異常。我嘗試過簡化田地,但似乎沒有任何幫助,有什麼想法?代碼與BB 6 SDK提供的Tables Demo類似。黑莓的Java的TableView:calculateVerticalScrollAmount異常?

它看起來像一個佈局的問題,但我似乎無法拖住錯誤。

// Create and apply style 
    RegionStyles style = new RegionStyles(BorderFactory.createSimpleBorder(new XYEdges(1, 1, 1, 1), Border.STYLE_SOLID), null, null, 
    null, RegionStyles.ALIGN_LEFT, RegionStyles.ALIGN_TOP); 

    // Create the view and controller 
    TableView tableView = new TableView(_tableModel); 
    TableController tableController = new TableController(_tableModel, tableView); 

    // Set the controller focus policy to highlight rows 
    tableController.setFocusPolicy(TableController.ROW_FOCUS); 

    // Set the behaviour of the controller when a table item is clicked 
    tableController.setCommand(new CommandHandler() 
    { 
     /** 
     * @see CommandHandler#execute(ReadOnlyCommandMetadata, Object) 
     */ 
     public void execute(ReadOnlyCommandMetadata metadata, Object context) 
     { 
      Dialog.alert("Command Executed"); 
     } 

    }, null, null); 

    tableView.setController(tableController); 

    // Create a DataTemplate that suppresses the third column 
    DataTemplate dataTemplate = new DataTemplate(tableView, 2, 3) 
    { 
     /** 
     * @see DataTemplate#getDataFields(int) 
     */ 
     public Field[] getDataFields(int modelRowIndex) 
     { 
      Object[] data = (Object[]) ((TableModel) getView().getModel()).getRow(modelRowIndex); 

      Field[] fields = new Field[4]; 
      fields[0] = new BitmapField((Bitmap) data[0]); 
      fields[1] = new LabelField(data[1], Field.FOCUSABLE); 
      fields[2] = new LabelField(data[2], Field.FOCUSABLE); 
      fields[3] = new LabelField(data[3], Field.FOCUSABLE); 

      return fields; 
     } 
    }; 

    // Set up regions 
    dataTemplate.createRegion(new XYRect(0, 0, 1, 2), style); 
    dataTemplate.createRegion(new XYRect(1, 0, 2, 1), style); 
    dataTemplate.createRegion(new XYRect(1, 1, 1, 1), style); 
    dataTemplate.createRegion(new XYRect(2, 1, 1, 1), style); 

    // Specify the size of each column by percentage, and the height of a row   
    dataTemplate.setColumnProperties(0, new TemplateColumnProperties(15, TemplateColumnProperties.PERCENTAGE_WIDTH)); 
    dataTemplate.setColumnProperties(1, new TemplateColumnProperties(15, TemplateColumnProperties.PERCENTAGE_WIDTH)); 
    dataTemplate.setColumnProperties(2, new TemplateColumnProperties(70, TemplateColumnProperties.PERCENTAGE_WIDTH)); 
    dataTemplate.setRowProperties(0, new TemplateRowProperties(ROW_HEIGHT)); 
    dataTemplate.setRowProperties(1, new TemplateRowProperties(ROW_HEIGHT)); 

    // Apply the template to the view 
    tableView.setDataTemplate(dataTemplate); 
    dataTemplate.useFixedHeight(true); 

    add(tableView); 

回答

4

不知道,如果你還在爲此苦苦掙扎 - 可能是爲他人

在聲明

DataTemplate dataTemplate = new DataTemplate(tableView, 2, 3) 

你有2行3列初始化的DataTemplate

收益(假設你想壓制第4欄)。但功能getDataFields內要返回4個領域。

這是導致飛機失事的原因(內部代碼是不是猴子的證明)。 下降,由陣列第四場,它應該工作。