根据从 XML 文件中解析信息的向量绘制 ListField 并使用 Graphics 绘制屏幕

发布于 2024-10-15 16:05:55 字数 1624 浏览 4 评论 0原文

   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 中运行时,执行此代码时会崩溃。

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

    }    
    }

The program complies correctly but when it runs in the Simulator 8800 it crashes it when this code is executed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-10-22 16:05:55

这段代码就是问题的根源:

catdrawer categorydraw = (catdrawer) this.get(list, index);

当categorydraw为null时,3行后调用drawtext会抛出异常。您需要检查是否为空。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文