Blackberry Java TableView: calculateVerticalScrollAmount Exception?-Collection of common programming errors


  • Antony Scott

    I’m getting a “TableView(ScrollView).calculateVerticalScrollAmount(XYRect)” exception when trying to create a table using the following code. I’ve tried simplifying the fields, but nothing seems to help, any thoughts? The code is similar to that in the Tables Demo supplied with the BB 6 SDK.

    It looks like a layout issue, but I can’t seem to pin down the error.

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

  • firebug

    Not sure if you are still struggling with this – may be benefits for others

    in the statement

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

    you have initialized the DataTemplate with 2 rows and 3 columns (assuming you want to suppress 4th column). But inside the function getDataFields you are returning 4 fields.

    This is causing the crash (the internal code is not monkey proof). Drop the 4th field from array and it should work.