简体   繁体   中英

Custom View class is throwing: java.lang.InstantiationException: java.lang.Class has no zero argument constructor

What I don't get is that it doesn't work no matter what, this is supposed to use the default constructors so they can't be left out or left empty. This isn't even the whole code I wrote, I separated the problem code from the rest. All I need is for it to stop crashing so I can test my app out. If someone can help me out here, I'd be very grateful

public class CustomView extends View {

private Paint background;
private Paint Lines;
private Paint blue, red, green, white;
private int rows, columns;
private Drawable board;

private boolean GameOver = false;

public CustomView(Context context) {
    super(context);

    init(null);
}

public CustomView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);

    init(attrs);

    background = new Paint();
    background.setColor(0);
    background.setStyle(Paint.Style.FILL);

    Lines = new Paint();
    Lines.setColor(0xffffff);
}

private void init(@Nullable AttributeSet attr) {
}


protected void onMeasure(int widthMeasure, int heightMeasure) {
    int width = MeasureSpec.getSize(widthMeasure);
    int height = MeasureSpec.getSize(heightMeasure);

    // Ensure the board is a square
    int dimension = Math.min(width, height);

    setMeasuredDimension(dimension, dimension);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    //we draw the board
    drawBoard(canvas);

}

private void drawBoard(Canvas canvas) {
    rows = 10;
    columns = 10;

    // We set the outline of the whole board
    canvas.drawRect(0, 0, getWidth(), getHeight(), background);

    float startx = 0;
    float starty = 0;

    float endx = getWidth() / columns;
    float endy = getWidth() / rows;


    //Here we draw the horizontal lines first
    for (int i = 0; i <= 9; i++) {
        starty = i * getHeight() / rows;
        canvas.drawLine(startx, starty, endx, endy, Lines);
    }

    //Now we draw the vertical lines
    for (int j = 0; j <= 9; j++) {
        startx = j * getWidth() / columns;
        canvas.drawLine(startx, starty, endx, endy, Lines);
    }
    //With this the squares should be drawn

}

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.cassandra_lee_2939561_minesweeper, PID: 31959 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.cassandra_lee_2939561_minesweeper/com.example.cassandra_lee_2939561_minesweeper.CustomView}: java.lang.InstantiationException: java.lang.Class<com.example.cassandra_lee_2939561_minesweeper.CustomView> has no zero argument constructor at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2841) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.Z93F725A07423 FE1C889F448B33D21F46Z:1696) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.InstantiationException: java.lang.Class<com.example.cassandra_lee_2939561_minesweeper.CustomView> has no zero argument constructor at Z93F725A07423FE1C889F448B33D21 F46Z.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1180) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.Z93F725A07423FE1C889F448B 33D21F46Z:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Never Mind, I fixed it myself thanks for the support

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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