繁体   English   中英

无法实例化课程; 没有空的构造函数

[英]can't instantiate class; no empty constructor

我在想要产生划痕效果的地方有以下代码:

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

我声明类的清单文件是:

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

当我想编译时出现上述错误:无法实例化类; 没有空的构造函数,我添加了一个空的构造函数,但没有成功

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

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

编辑

测试; 没有空的构造函数

空的构造函数是这样的:

public Test() {}

但是闻起来您尝试以错误的方式使用此视图。 您不是试图将它用作片段吗?

您只有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);
    }

您的测试班只有一个构造函数

这个:

public Test(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

您需要默认值(构造函数不带参数)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM