java - 如何修复 int Bitmap getWidth() 的空引用错误
问题描述
我更新了我的 PaintView 代码,因为根据我的 android studio 错误日志,它说我需要扩展到 android.app.activity 但现在我的位图 getwidth() 中有一个空引用错误。我是 android 应用程序的新手,但据我了解,它没有返回对象,因为它找不到位图?这是我的 PaintView 类:
public class PaintView extends android.app.Activity{
LayoutParams params;
Path path = new Path();
Paint br = new Paint();
Bitmap background;
Bitmap background2;
public PaintView(Context context) {
super();
int maxHeight = 2000;
int maxWidth = 1000;
float scale = Math.min(((float) maxHeight / background.getWidth()), ((float) maxWidth / background.getHeight()));
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
background = Bitmap.createBitmap(background, 0, 0, background.getWidth(), background.getHeight(), matrix, true);
background2 = Bitmap.createBitmap(background2, 0, 0, background2.getWidth(), background2.getHeight(), matrix, true);
br.setColor(Color.GREEN);
br.setStyle(Paint.Style.STROKE);
br.setStrokeJoin(Paint.Join.ROUND);
br.setStrokeWidth(10f);
params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
private float getHeight() {
int height = 5;
return height;
}
private float getWidth() {
int width = 5;
return width;
}
//@Override
public void onClick(View view) {
Log.i("Hello", "Hello you!");
}
}
解决方案
bitmap
在分配它之前,您在类的构造函数中使用它。
float scale = ... background.getWidth() ...
...
background = Bitmap.createBitmap(...)
推荐阅读
- android - Android Studio“忘记”布局文件存在
- http - 使用 Http 获取请求,使用 WebSocket 给出结果
- java - 使用 Java 开发工具包在代码中嵌入 AWS IAM 凭证
- r - R - 从 json 中提取数据
- python - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 运行代码时出现此错误
- react-native - react-native 捆绑图像路径
- c++ - 以优雅的方式在游戏引擎中管理指向已销毁对象的指针
- amazon-web-services - AWS S3 存储的对象锁定设置不起作用
- asp.net-core - 自定义错误页面 .Net Core Web 应用程序未显示 500 错误
- docker - 在 windows 容器中托管后无法通过浏览器浏览 Asp.net core web api