重写surfaceView时出现异常
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
出错的代码块如下:
public ProgressView(Context context) {
super(context);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
holder = this.getHolder();
holder.addCallback(this);
paint1 = new Paint();
paint1.setColor(Color.RED);
paint2 = new Paint();
paint2.setColor(Color.GRAY);
Bitmap bitmap1 = BitmapFactory.decodeStream(context.getResources()
.openRawResource(R.drawable.ic_launcher));
bitmap = bitmap1.extractAlpha();// 获取一个透明图片
y = bitmap.getWidth();//初始化y轴坐标
}
解决方案:
<span style="font-size:18px;">public ProgressView(Context context,<span style="color:#ff0000;">AttributeSet attrs</span>) {
super(context<span style="color:#ff0000;">,attrs</span>);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
holder = this.getHolder();
holder.addCallback(this);
paint1 = new Paint();
paint1.setColor(Color.RED);
paint2 = new Paint();
paint2.setColor(Color.GRAY);
Bitmap bitmap1 = BitmapFactory.decodeStream(context.getResources()
.openRawResource(R.drawable.ic_launcher));
bitmap = bitmap1.extractAlpha();// 获取一个透明图片
y = bitmap.getWidth();//初始化y轴坐标
}</span>