这个android代码有什么问题?

时间:2014-04-29 03:28:53

标签: android

当我尝试运行此代码时,我在视图活动中收到一条错误,指出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);
    }
}
}

1 个答案:

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