Drawing a ListField and painting the screen with Graphics based on a vector parsing information from an XML file
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() {
开发者_StackOverflow社区 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();
}
}
The program complies correctly but when it runs in the Simulator 8800 it crashes it when this code is executed.
This code is the source of the problem:
catdrawer categorydraw = (catdrawer) this.get(list, index);
When categorydraw is null, the call to drawtext 3 lines later will throw an exception. You need to check for null.
精彩评论