【问题标题】:Unable to run Drawing app CodeName one无法运行绘图应用程序代号一
【发布时间】:2025-12-12 02:30:01
【问题描述】:

我扩展了组件类并重写了合适的方法来实现绘图应用程序,例如pointerPressed() 和paintBackground()。代码编译成功,但当我尝试在模拟器上的类声明行中运行它时会出现 NullPointerException。

以下是代码:-

package com.mycompany.myapp;
import com.codename1.io.Log;
import com.codename1.ui.Button;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.Graphics;
import com.codename1.ui.Image;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.Painter;
import com.codename1.ui.Stroke;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.geom.GeneralPath;
import com.codename1.ui.geom.Rectangle;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.layouts.FlowLayout;
import com.codename1.ui.layouts.LayeredLayout;
import com.codename1.ui.painter.PainterChain;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.ui.util.UITimer;
import java.io.IOException;
import com.codename1.ui.Toolbar;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class PaintingActivity extends Component{

    private Form current;
    private Resources theme;
    GeneralPath p = new GeneralPath();
    int strokeColor = 0x00000ff;
    int strokeWidth = 10;
    private float lastX = -1;
    private float lastY = -1;
    private boolean odd = true;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
    }

    public void start() {
        if(current != null){
            current.show();
            return;
        }

        Form hi = new Form("Paint");
       // DrawingCanvas dc = new DrawingCanvas();
        //hi.add(dc);
        hi.setGlassPane(new Painter() {

            @Override
            public void paint(Graphics g, Rectangle rect) {
                // TODO Auto-generated method stub
                g.setColor(0x0000ff);
                g.fillRect(hi.getX(), hi.getY(), hi.getWidth(), hi.getHeight());
            }
        });
//        hi.addPointerPressedListener(new ActionListener<ActionEvent>() {
//          @Override
//          public void actionPerformed(ActionEvent evt) {
//              dc.pointerPressed(evt.getX(), evt.getY());      
//              //hi.add(dc.l1);
//              //hi.add(dc.l2);
//          }
//      });
        hi.show();

    }

    public void addPoint(float x, float y){
        Log.p("addPoint() starts");
        if ( lastX == -1 ){
            p.moveTo(x, y);

        } else {
            float controlX = odd ? lastX : x;
            float controlY = odd ? y : lastY;
            p.quadTo(controlX, controlY, x, y);
        }
        odd = !odd;
        lastX = x;
        lastY = y;
        //l1 = new Label("addPoint()");
        repaint();
        Image mutable = Image.createImage(320,480);
        paintBackground(mutable.getGraphics());
        Log.p("addPoint() ends");
    }

    @Override
    public void paintBackground(Graphics g) {
        super.paintBackground(g);
        Log.p("paintBackgrounds() starts");
        g.setColor(0x0000ff);
        g.fillRect(getX(), getY(), getWidth(), getHeight());

        if(g.isShapeSupported())
                Log.p("supported");
        else
            Log.p("not supported");
       // g.drawLine(100, 100, 200, 200);
            Stroke stroke = new Stroke(
                strokeWidth,
                Stroke.CAP_BUTT,
                Stroke.JOIN_ROUND, 1f
            );
            g.setColor(strokeColor);
            g.drawRect(100, 100,100, 100);
            Log.p("After rect");
            // Draw the shape
            g.drawShape(p, stroke);


        Log.p("paintBackgrounds() ends");
    }

    @Override
    public void pointerPressed(int x, int y) {
        Log.p("pointerPressed()");
        addPoint(x-getParent().getAbsoluteX(), y-getParent().getAbsoluteY());
    }

    public void stop() {
        current = Display.getInstance().getCurrent();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = Display.getInstance().getCurrent();
        }
    }

    public void destroy() {
    }

}

日志:-

java.lang.NullPointerException
    at com.codename1.ui.Font.<init>(Font.java:148)
    at com.codename1.ui.Font.createSystemFont(Font.java:372)
    at com.codename1.ui.plaf.UIManager.resetThemeProps(UIManager.java:326)
    at com.codename1.ui.plaf.UIManager.<init>(UIManager.java:98)
    at com.codename1.ui.plaf.UIManager.getInstance(UIManager.java:116)
    at com.codename1.ui.Component.getUIManager(Component.java:488)
    at com.codename1.ui.Component.<init>(Component.java:428)
    at com.gurankas.drawingapp.MainClass.<init>(MainClass.java:19)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at com.codename1.impl.javase.Executor$1.run(Executor.java:105)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

主类代码:-

package com.gurankas.drawingapp;


import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Component;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class MainClass extends Component{

    private Form current;
    private Resources theme;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
    }

    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World");

        hi.addComponent(new Label("Hi World"));
        hi.show();
    }

    public void stop() {
        current = Display.getInstance().getCurrent();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = Display.getInstance().getCurrent();
        }
    }

    public void destroy() {
    }

}

请忽略注释掉的部分,因为它们仅用于测试功能。任何早期答复表示赞赏。谢谢

【问题讨论】:

  • 在最近的一个发现中,我创建了一个简单的新 Hello world 项目,并且只是从 com.codename.ui.Component 扩展了 Component。它给出了相同的 NullPointerException。

标签: codenameone


【解决方案1】:

您的例外来自您的 MainClass 文件第 19 行,您在该文件中派生了一个组件,但没有调用 setUIID() 来设置默认组件 UIID。

【讨论】:

  • 我不明白。 MainClass 是 CN1 生成的文件,唯一的变化是“扩展组件”。我应该使用 setUIID() 方法做什么?我已经更新了问题中的 MainClass 代码以供参考
  • 你不能改变主类来扩展一些东西。尤其不是Component。我建议在开发者指南中查看一些示例,例如绘图示例和时钟示例
  • 这解释了很多。谢谢。
最近更新 更多