我扩展了组件类并重写了合适的方法来实现绘图应用程序,如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)
MainClass代码: -
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() {
}
}
请忽略已注释掉的部分,因为它们仅用于测试功能。任何早期回复表示赞赏。感谢
答案 0 :(得分:1)
您的例外来自MainClass
文件第19行,您在其中派生了一个组件,但未调用setUIID()
来设置默认组件UIID。