public class CategoriesUI extends MainScreen implements ListFieldCallback {
//categoryimport.listingnodup is the current categories with no duplicates
public Categories categoryimport = new Categories(); //brings in all infromation from Categories.java
private ListField allcategories;
CategoriesUI() {
this.add(new LabelField("List of Categories"));
allcategories = new ListField(categoryimport.listingnodup.size());
allcategories.setCallback(this); //we manage the interaction!
this.add(allcategories);
}
protected boolean onSavePrompt() {
return true;
}
//Implemented Call Back Methods follow
//draw the current row
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
catdrawer categorydraw = (catdrawer) this.get(list, index);
int drawColor = Color.BLACK;
g.setColor(drawColor);
g.drawText(categorydraw.cat, 0, y, 0, w);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
//Not a current implementation this is really just commented out
return start;
}
//Returns the object at the specified index
public Object get(ListField list, int index){
return categoryimport.listingnodup.elementAt(index);
}
class catdrawer {
public String cat = categoryimport.listingnodup.toString();
}
}
该程序正确符合要求,但当它在 Simulator 8800 中运行时,执行此代码时会崩溃。
最佳答案
此代码是问题的根源:
catdrawer categorydraw = (catdrawer) this.get(list, index);
当categorydraw为null时,3行后调用drawtext会抛出异常。您需要检查是否为空。
关于java - 根据从 XML 文件中解析信息的 vector 绘制 ListField 并使用 Graphics 绘制屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4857646/