Surfaceview绘制线程错误NULLPointerException

时间:2017-05-18 08:47:41

标签: java android multithreading nullpointerexception surfaceview

我是android的初学者。我创建了一个简单的表面视图来绘制一个线程(绘制矩形),但它不断出现这样的错误:

  

java.lang.NullPointerException:尝试调用虚方法' void android.graphics.Canvas.drawColor(int)'在空对象引用上

这是我的代码:

myActivity.java:

public class myActivity extends AppCompatActivity {

mySurfaceView mysurfaceview;

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

    //mysurfaceview = new mySurfaceView(this);
    mysurfaceview = (mySurfaceView) findViewById(R.id.mySurfaceView);
    mysurfaceview.RunThread();
 }

mySurfaceview.java:

public class mySurfaceView extends SurfaceView implements SurfaceHolder.Callback {

public DrawThread DrawThread;
private Rect myrectangle;
private Paint myPaint;

int moveX = 0;

private void init() {
    myrectangle = new Rect(150, 150, 700, 500);
    myPaint = new Paint();
    myPaint.setColor(Color.BLUE);
}

public mySurfaceView(Context context) {
    super(context);
    //init();
}

public mySurfaceView(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); }

public mySurfaceView(Context context, AttributeSet attrs) { super(context, attrs); }

public void onDrawSomething(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    canvas.drawRect(myrectangle, myPaint);
}

@Override
public void surfaceCreated(SurfaceHolder holder) { }

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

@Override
public void surfaceDestroyed(SurfaceHolder holder) { }

public void update(){
}

public void RunThread() {
    init();
    DrawThread = new DrawThread(getHolder(), this);
    DrawThread.setRunning(true);
    DrawThread.start();
}

public class DrawThread extends Thread {
    //var definition
    public boolean run = false;
    private SurfaceHolder surfaceHolder;
    mySurfaceView mysurfaceView;

    public DrawThread(SurfaceHolder surfaceHolder, mySurfaceView mysurfaceView) {
        this.surfaceHolder = surfaceHolder;
        this.mysurfaceView = mysurfaceView;
    }

    public void setRunning(boolean run) {
        this.run = run;
    }

    @Override
    public void run() {
        while(run)
        {
            //implement fps counter
            Canvas canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    mysurfaceView.onDrawSomething(canvas);
                    //mysurfaceView.update();
                }
            } finally {
                if(canvas != null)
                {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

}

这是我的XML代码,用于显示表面视图:

 <com.example.firstapp_aurelia.mySurfaceView
        android:id="@+id/mySurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

我在这里做错了吗?非常感谢你。

1 个答案:

答案 0 :(得分:0)

我没有运行代码但是没有初始化mySurfaceView两次

mysurfaceview = new mySurfaceView(this);
mysurfaceview = (mySurfaceView) findViewById(R.id.mySurfaceView);