【发布时间】:2014-10-27 20:43:23
【问题描述】:
我正在尝试打开位于路径“sketchfolder/data/1024x768/gulli2.png”的图像。它在 Windows 上运行良好,但在 Android 上无法运行并出现错误。这是错误:
找不到图像 \1024x768\gulli2.png。致命例外: 动画线程进程:processing.test.exercise_5_3_noisechain, PID: 11878 java.lang.NullPointerException 在 processing.test.exercise_5_3_noisechain.Exercise_5_3_NoiseChain$Gulli.(Exercise_5_3_NoiseChain.java:112) 在 processing.test.exercise_5_3_noisechain.Exercise_5_3_NoiseChain.setup(Exercise_5_3_NoiseChain.java:37) 在 processing.core.PApplet.handleDraw(Unknown Source) 在 processing.core.PGraphicsAndroid2D.requestDraw(未知来源)在 processing.core.PApplet.run(未知来源)在 java.lang.Thread.run(Thread.java:841)
我正在尝试的代码:
class Gulli {
Body body;
float w;
float h;
float angle;
PImage img;
Gulli(float x_, float y_) {
float x = x_;
float y = y_;
w = (float)(width*2.2/100.0);
h = w/2;
img = loadImage("\\1024x768\\gulli2.png");
img.loadPixels();
img.resize((int)w*2,0);
img.updatePixels();
angle = 0;
makeBody(new Vec2(x, y), w, h, angle);
}
void makeBody(Vec2 center, float w_, float h_, float a) {
BodyDef bd = new BodyDef();
bd.position.set(box2d.coordPixelsToWorld(center));
bd.setAngularVelocity(a);
bd.type = BodyType.DYNAMIC;
bd.bullet = true;
body = box2d.createBody(bd);
PolygonShape sd = new PolygonShape();
Vec2 []vertices = new Vec2[6];
vertices[0] = new Vec2(box2d.scalarPixelsToWorld(w), 0);
vertices[1] = new Vec2(box2d.scalarPixelsToWorld(h), box2d.scalarPixelsToWorld(h/3));
vertices[2] = new Vec2(box2d.scalarPixelsToWorld(-h), box2d.scalarPixelsToWorld(h/3));
vertices[3] = new Vec2(box2d.scalarPixelsToWorld(-w), 0);
vertices[4] = new Vec2(box2d.scalarPixelsToWorld(-h), box2d.scalarPixelsToWorld(-h/3));
vertices[5] = new Vec2(box2d.scalarPixelsToWorld(h), box2d.scalarPixelsToWorld(-h/3));
sd.set(vertices, vertices.length);
FixtureDef fd = new FixtureDef();
fd.shape = sd;
fd.density = 4.0;
fd.friction = 0.6;
fd.restitution = 0.3;
body.createFixture(fd);
bd.allowSleep = false;
body.setUserData(this);
}
void display() {
Vec2 pos = box2d.getBodyPixelCoord(body);
float a = body.getAngle();
Fixture f = body.getFixtureList();
PolygonShape ps = (PolygonShape) f.getShape();
pushMatrix();
translate(pos.x, pos.y);
rotate(-a);
image(img, 0, 0);
popMatrix();
}
}
import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
Box2DProcessing box2d;
Gulli gulli;
void setup() {
// size(1024, 768); // turn on for windows
box2d = new Box2DProcessing(this);
box2d.createWorld();
gulli = new Gulli((float)width*15.0/100.0, (float)height*95.0/100.0);
smooth();
}
void draw() {
gulli.display();
box2d.step();
}
请帮忙..谢谢..
【问题讨论】:
-
Exercise_5_3_NoiseChain.java 的第 12 行在哪里?
-
对不起我的错误。我忘了提 gulli 的 display() 函数。我现在已经提到了。请现在检查。谢谢:)
-
你在说哪一行?有什么错误吗?
-
(Exercise_5_3_NoiseChain.java:112)这部分错误信息的意思是问题出现在Exercise_5_3_NoiseChain.java的第112行...哎呀,我之前输入了12...那么哪一行代码是112呢? -
没有第 112 行。错误是我正在加载图像的位置。我给出的路径不适用于android。但它在 Windows 上工作正常
标签: android processing