[英]can't instantiate class; no empty constructor
I have the following code where I want to make a scratch effect: 我在想要产生划痕效果的地方有以下代码:
public class Test extends View {
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
private boolean isMove = false;
private Bitmap bitmap = null;
private Bitmap frontBitmap = null;
private Path path;
private Canvas mCanvas;
private Paint paint;
protected void onDraw(Canvas canvas) {
if (mCanvas == null) {
EraseBitmp();
}
canvas.drawBitmap(bitmap, 0, 0, null);
mCanvas.drawPath(path,paint);//Draw a path
super.onDraw(canvas);
}
public void EraseBitmp() {
bitmap = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_4444);
frontBitmap = CreateBitmap(Color.GRAY,getWidth(),getHeight());//The production of grey.
//Set the brush style
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);//Set the brush style: Hollow
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//Display mode settings when photo cross
paint.setAntiAlias(true);//Anti according to tooth
paint.setDither(true);//Anti jitter
paint.setStrokeJoin(Paint.Join.ROUND);//Set the joint appearance, ROUND arc
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(20);//Set the stroke width
path = new Path();
mCanvas = new Canvas(bitmap);//Set up a band picture of canvas
mCanvas.drawBitmap(frontBitmap, 0, 0,null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float ax = event.getX();
float ay = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isMove = false;
path.reset();
path.moveTo(ax, ay);
invalidate();
return true;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
isMove = true;
path.lineTo(ax,ay);
invalidate();
return true;
}
return super.onTouchEvent(event);
}
public Bitmap CreateBitmap(int color,int width, int height) {
int[] rgb = new int [width * height];
for (int i=0;i<rgb.length;i++) {
rgb[i] = color;
}
return Bitmap.createBitmap(rgb, width, height,Config.ARGB_8888);
}
}
and the manifest file where i declare the class is: 我声明类的清单文件是:
<activity
android:name=".Test"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
When i want to compile is gives the above error: can't instantiate class; 当我想编译时出现上述错误:无法实例化类; no empty constructor I added an empty constructor but no success.
没有空的构造函数我添加了一个空的构造函数,但没有成功。 I am a newbie so please be patient.
我是新手,所以请耐心等待。 I searched the net but couldn't get a solution.
我在网上搜索,但找不到解决方案。
And the xml: 和xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="100dp"
>
<TextView
android:id="@+id/textView1"
android:layout_width="300dp"
android:layout_height="150dp"
android:textSize="40dp"
android:gravity="center_vertical|center_horizontal"
android:text="Thanks for your patronage"
android:background="#000"
/>
<com.example.winter.carrefour.Test
android:id="@+id/eraseView1"
android:layout_width="300dp"
android:layout_height="150dp"/>
</RelativeLayout>
</LinearLayout>
When i want to compile is gives the above error: can't instantiate class;
当我想编译时出现上述错误:无法实例化类; no empty constructor I added an empty constructor but no success
没有空的构造函数,我添加了一个空的构造函数,但没有成功
There's no required constructor in code you posted. 您发布的代码中没有必需的构造函数。 You got only one, while you need three:
您只有一个,而您需要三个:
public Test(Context context) {
super(context);
}
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Test(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
EDIT 编辑
Test;
测试; no empty constructor
没有空的构造函数
Empty constructor is this: 空的构造函数是这样的:
public Test() {}
however it smells that you try to use this View wrong way. 但是闻起来您尝试以错误的方式使用此视图。 Aren't you trying to use it as fragment by any chance?
您不是试图将它用作片段吗?
You have only 1 constructor create additional ones and will be fine 您只有1个构造函数,可以创建其他构造函数,这样就可以了
public Test(Context context) {
super(context);
}
public Test(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Test(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
Your Test class has only one Constructor 您的测试班只有一个构造函数
this: 这个:
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
you need the default one (the constructor taking no parameters) 您需要默认值(构造函数不带参数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.