简体   繁体   English

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

[英]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.

相关问题 Android:无法实例化类:没有空构造函数 - Android: can't instantiate class : no empty constructor 无法实例化类......; 没有空的构造函数 - Can't instantiate class …; no empty constructor java.lang.InstantiationException:无法实例化类; 没有空的构造函数 - java.lang.InstantiationException: can't instantiate class; no empty constructor 无法实例化课程; 没有空的构造函数错误-Android切换活动 - Can't instantiate class; no empty constructor error - Android switching activities Android-java.lang.InstantiationException:无法实例化类:无空构造函数 - Android - java.lang.InstantiationException: can't instantiate class : no empty constructor Android在重新启动应用程序时崩溃:java.lang.InstantiationException:无法实例化类; 没有空的构造函数 - Android crash on restart app : java.lang.InstantiationException: can't instantiate class ; no empty constructor Android通过其他类java.lang.InstantiationException中的线程调用函数:无法实例化类……没有空的构造函数 - Android invoke a Function via Thread in other Class java.lang.InstantiationException: can't instantiate class … no empty constructor 即使存在类和构造函数也无法实例化类型 - Can't instantiate type even though class and constructor exist 为什么不能在构造函数中实例化该类的相同对象? - Why can't you instantiate the same object of that class inside constructor? Java 反射无法实例化没有 arg 构造函数的私有嵌套类 - Java reflection can't instantiate a private nested class with no arg constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM