尝试使用以下代码创建表格时,出现“TableView(ScrollView).calculateVerticalScrollAmount(XYRect)”异常。我尝试过简化字段,但似乎没有任何帮助,有什么想法吗?该代码与 BB 6 SDK 提供的 Tables Demo 中的代码类似。
看起来像是布局问题,但我似乎无法确定错误。
// 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);
最佳答案
不确定您是否仍在为此苦苦挣扎 - 可能对其他人有好处
在声明中
DataTemplate dataTemplate = new DataTemplate(tableView, 2, 3)
您已初始化具有 2 行和 3 列的 DataTemplate(假设您要抑制第 4 列)。但在函数 getDataFields
中,您将返回 4 个字段。
这导致了崩溃(内部代码不是猴子证明)。 从数组中删除第四个字段,它应该可以工作。
关于java - 黑莓 Java TableView : calculateVerticalScrollAmount Exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7178257/