【问题标题】:drawBitmap context.getResource nulldrawBitmap context.getResource null
【发布时间】: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


【解决方案1】:

我解决了我的错误,只是在我的方法中添加了 Context

    private static void draw(Canvas canvas, String letters, int tileColor,
                int textColor, int left, int top, int right, int bottom, Context context) {
                   //my code

}

谢谢大家

【讨论】:

  • 您可以通过单击绿色对勾“接受”您自己的答案。这会将其从 StackOverflow 上未回答的问题池中删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多