自定义视图类正在抛出:java.lang.instantiationexception:java.lang.class没有零参数构造函数

u5rb5r59  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(264)

我不明白的是,不管怎样它都不工作,这应该使用默认构造函数,这样它们就不能被遗漏或留空。这甚至不是我写的全部代码,我把问题代码和其他代码分开了。我只需要它停止崩溃,这样我就可以测试我的应用程序了。如果有人能帮我,我会非常感激的

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:致命异常:主进程:com.example.cassandra\u lee\u 2939561\u扫雷舰,pid:31959 java.lang.runtimeexception:无法示例化活动组件信息{com.example.cassandra\u lee\u 2939561\u minesweeper/com.example.cassandra\u lee\u 2939561\u minesweeper.customview}:java.lang.instantiationexception:java.lang.class<com.example.cassandra\u lee\u 2939561\u minesweeper.customview>在android.app.activitythread.performlaunchactivity(活动线程。java:2841)在android.app.activitythread.handlelaunchactivity(activitythread。java:3032)在android.app.activitythread.-wrap11(未知)source:0)在android.app.activitythread$h.handlemessage上。java:1696)在android.os.handler.dispatchmessage(handler。java:105)在android.os.looper.loop(looper。java:164)在android.app.activitythread.main(activitythread。java:6944)java.lang.reflect.method.invoke(本机方法)com.android.internal.os.zyote$methodandargscaller.run(zyote)。java:327)在com.android.internal.os.zygoteinit.main(zygoteinit。java:1374)原因:java.lang.instantiationexception:java.lang.class<com.example.cassandra\u lee\u 2939561\u minesweeper.customview>在android.app.instrumentation.newactivity(instrumentation)的java.lang.class.newinstance(本机方法)中没有零参数构造函数。java:1180)在android.app.activitythread.performlaunchactivity(activitythread。java:2831)在android.app.activitythread.handlelaunchactivity(活动线程。java:3032)  在android.app.activitythread.-wrap11(未知)source:0)  在android.app.activitythread$h.handlemessage上。java:1696)  在android.os.handler.dispatchmessage(handler。java:105)  在android.os.looper.loop(looper。java:164)  在android.app.activitythread.main(activitythread。java:6944)  位于java.lang.reflect.method.invoke(本机方法)  在com.android.internal.os.zyote$methodandargscaller.run(zyote。java:327)  在com.android.internal.os.zygoteinit.main(zygoteinit。java:1374) 

8cdiaqws

8cdiaqws1#

没关系,我自己修好的谢谢你的支持

相关问题