-2
时NullPointeerException朝着这条线NullPointerException异常试图加载图像
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
public class Painter {
private Canvas canvas;
private Paint paint;
private Rect srcRect;
private Rect dstRect;
private RectF dstRectF;
public Painter(Canvas canvas) {
this.canvas = canvas;
paint = new Paint();
srcRect = new Rect();
dstRect = new Rect();
dstRectF = new RectF();
}
public void setColor(int color) {
paint.setColor(color);
}
public void setFont(Typeface typeface, float textSize) {
paint.setTypeface(typeface);
paint.setTextSize(textSize);
}
public void drawString(String str, int x, int y) {
canvas.drawText(str, x, y, paint);
}
public void fillRect(int x, int y, int width, int height) {
dstRect.set(x, y, x + width, y + height);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(dstRect, paint);
}
public void drawImage(Bitmap bitmap, int x, int y) {
// NullPointerException here canvas.drawBitmap(bitmap, x, y, paint);
}
public void drawImage(Bitmap bitmap, int x, int y, int width, int height) {
srcRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
dstRect.set(x, y, x + width, y + height);
canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
}
public void fillOval(int x, int y, int width, int height) {
paint.setStyle(Paint.Style.FILL);
dstRectF.set(x, y, x + width, y + height);
canvas.drawOval(dstRectF, paint);
}
因为文件存储在该项目应该不为空指出。我可以看到问题出在哪里的唯一方法是因为我从复制的初始框架将所有图片保存在一个名为assets的独立文件夹中,该文件夹显然不是默认文件夹。相反,我将所有资源放在可绘制文件夹中。究竟是什么问题?
Logcat
11-06 10:21:55.114 25146-25168/rect.draw.gametest E/AndroidRuntime﹕ FATAL EXCEPTION: Game Thread
Process: rect.draw.gametest, PID: 25146
java.lang.NullPointerException
at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1083)
at android.graphics.Canvas.drawBitmap(Canvas.java:1139)
at rect.draw.gametest.model.util.Painter.drawImage(Painter.java:45)
at rect.draw.gametest.model.state.MenuState.render(MenuState.java:28)
at rect.draw.gametest.GameView.updateAndRender(GameView.java:100)
at rect.draw.gametest.GameView.run(GameView.java:119)
at java.lang.Thread.run(Thread.java:841)
Plaese,显示更多代码并打印logcat –
如果在代码中从一行中抛出NPE,您可以尝试在该行上设置断点以查看哪些内容为null,然后从那里回溯。我猜可能在这里画布 - 但正如亚瑟所说,我们确实需要看到更多的代码。 –
@ArthurKorchagin我增加了更多的代码。这是更多的空白点,但我想我可以解决其余的,如果我认为主要指出。 –