当我尝试运行此代码时,我在视图活动中收到一条错误,指出void是函数的无效类型" protected void onDraw(Canvas canvas)"。
package com.example.crazyeights;
import android.app.Activity;
import android.os.Bundle;
public class CrazyEightsActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
CrazyEightsView myView = new CrazyEightsView(this);
setContentView(myView);
}
}
package com.example.crazyeights;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class CrazyEightsView extends View
{
private Paint redPaint;
private int circleX;
private int circleY;
private float radius;
public CrazyEightsView(Context context)
{
super(context);
redPaint = new Paint();
redPaint.setAntiAlias(true);
redPaint.setColor(Color.RED);
circleX = 100;
circleY = 100;
radius = 30;
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawCircle(circleX, circleY, radius, redPaint);
}
}
}
答案 0 :(得分:0)
将onDraw移出构造函数。
public CrazyEightsView(Context context)
{
super(context);
redPaint = new Paint();
redPaint.setAntiAlias(true);
redPaint.setColor(Color.RED);
circleX = 100;
circleY = 100;
radius = 30;
} // <-- put } here
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawCircle(circleX, circleY, radius, redPaint);
}
//} <-- used to be here