【发布时间】:2014-08-04 16:02:11
【问题描述】:
您好,我正在尝试使用 Bitmap 设置图像,但在我的上下文中有 Null 点异常。
这是我的代码。
public class Helper extends Activity {
//my code
private static Helper _instance;
public Helper() {
_instance = this;
}
public static Context getContext() {
return _instance;
}
//code
private static void draw(Canvas canvas, String letters, int tileColor,
int textColor, int left, int top, int right, int bottom) {
//code
canvas.drawBitmap(BitmapFactory.decodeResource(_instance.getResources(),
R.drawable.wine), (right + left) / 2 - width / 2, (top + bottom)
/ 2 + rect.height() / 2, null);
}
我做错了什么?我的错误在 _instance.getResources()
【问题讨论】:
-
在哪里新建
Helper -
你的意思是Helper h = new helper();?我不这样做。
-
那么这就是为什么
_instance为空 -
所以在使用 _instance 之前我需要做 Helper h = new helper();然后使用 h.getResources()?
-
嗯,是的,当你不初始化任何语言的对象为空的东西时
标签: android canvas nullpointerexception android-context