gpt4 book ai didi

java - 无法实例化类;没有空构造函数

转载 作者:行者123 更新时间:2023-12-01 20:53:46 24 4
gpt4 key购买 nike

我有以下代码,我想在其中制作划痕效果:

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);
}
}

我声明该类的 list 文件是:

  <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>

当我想编译时出现上述错误:无法实例化类;没有空构造函数我添加了一个空的构造函数但没有成功。我是新手所以请耐心等待。我在网上搜索但没有找到解决方案。

和 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

您发布的代码中没有必需的构造函数。你只有一个,而你需要三个:

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);
}

编辑

Test; no empty constructor

空构造函数是这样的:

public Test() {}

但是,您似乎尝试以错误的方式使用此 View 。您是否尝试将其用作 fragment ?

关于java - 无法实例化类;没有空构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42715528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com