无法将圆圈绘制到屏幕上

时间:2016-09-14 03:37:31

标签: java android

我是Android编程新手,我试图在屏幕上画一个圆圈。最后,我想让圈子四处移动。但是现在我一直无法让圆圈画出来。当我运行代码时,它给了我一个错误:

  

"尝试调用虚拟方法' long   android.graphics.Paint.getNativeInstance()'在null"

我的代码:

package com...

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }

    class MyView extends View {



    private Bitmap b;
        public Paint p;

        public MyView(Context context) {
            super(context);
            Paint p = new Paint();
            p.setColor(Color.GREEN);
            b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawCircle(50, 50, 10, p);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码存在问题

v0.10.30