画布上绘制方法

时间:2016-02-02 08:45:51

标签: android canvas

我是Android编程新手,我正试图从屏幕上创建一个绿色圆圈图像。我尝试使用画布绘制,但它总是显示错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Paint.setColor(int)' on a null object reference
            at com.example.jordanong09.handeyegame.GameActivity.onDraw(GameActivity.java:61)
            at com.example.jordanong09.handeyegame.GameActivity.onCreate(GameActivity.java:47)

以下是我的程序代码:

public class GameActivity extends Activity {
    TextView textViewTime, textViewScore;
    int timeLeft,score;
    float xcoord,ycoord,radius = 100;
    Paint paint;
    Canvas canvas1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);


        String timeData = getIntent().getExtras().getString("timeDuration");
        if(timeData == "30sec"){
            timeLeft = 30;
        }

        textViewTime = (TextView)findViewById(R.id.textViewTime);
        textViewScore = (TextView)findViewById(R.id.textViewScore);
        textViewTime.setText(timeData);
        final CounterClass timer = new CounterClass (30000, 1000);
        timer.start();
        onDraw(canvas1);

    }

    protected void onDraw (Canvas canvas)
    {
        Display display = getWindowManager().getDefaultDisplay();
        Point screenSize = new Point();
        display.getSize(screenSize);
        int width = screenSize.x;
        int height = screenSize.y;
        Random random = new Random();
        xcoord = random.nextFloat() * width;
        ycoord = random.nextFloat() * height;
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(xcoord, ycoord, radius, paint);
    }

我真的不知道它有什么问题,因为我一直在网上研究解决方案。希望我能听到一些建议。感谢

2 个答案:

答案 0 :(得分:0)

未正确初始化Paint对象。你应该打电话给

paint = new Paint();

之前打电话

paint.setColor();

否则paint为null,并且您无法在null上调用方法。

答案 1 :(得分:0)

您应该创建自定义视图并将其作为子视图添加到您的某个视图组中。这是一个示例代码。希望这会有所帮助。

/**
 * Created by sanjeet on 2/2/16.
 */
public class GameActivity extends AppCompatActivity {
    TextView textViewTime, textViewScore;
    int timeLeft, score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        textViewTime = (TextView)findViewById(R.id.textViewTime);
        textViewScore = (TextView)findViewById(R.id.textViewScore);
        /*Create a ViewGroup in your layout to contain this custom GameView instance */
        LinearLayout gameViewContainer = (LinearLayout)findViewById(R.id.custom_view_container);
        gameViewContainer.addView(new GameView(this));

    }



    class GameView extends View {

       Paint paint;
       int width;
       int height;
       private float radius=100;
       Random random;
       public GameView(Context context) {
           super(context);
           paint = new Paint();
           paint.setColor(Color.GREEN);
           paint.setStyle(Paint.Style.STROKE);
           Display display = getWindowManager().getDefaultDisplay();
           Point screenSize = new Point();
           display.getSize(screenSize);
           width = screenSize.x;
           height = screenSize.y;
           random = new Random();
       }


       @Override
       protected void onDraw(Canvas canvas) {
           super.onDraw(canvas);
           float xcoord = random.nextFloat() * width;
           float ycoord = random.nextFloat() * height;
           canvas.drawCircle(xcoord, ycoord, radius, paint);

       }

   }


}