我在SurfaceView.updateWindow中得到一个空指针异常,我猜它是因为我在onDraw但是从未在表面上接收过surfaceCreated。那么有没有办法解决这个问题,或者在直接渲染到位图时强制使用surfacecreated来调用它。
此表面视图在作为活动中的内容视图添加时正确呈现。但实际上我想将surfaceview直接渲染到位图而不在任何地方显示它。
SurfaceView构造函数:
public ObservationDetailsGraphicView(Context context) {
super(context);
getHolder().addCallback(this);
}
SurfaceView onDraw:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
LayoutInflater inflater = LayoutInflater.from(c);
View detailView = inflater.inflate(R.layout.observation_details_graphic_view,null);
detailView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
detailView.measure(bmWidth, bmHeight);
detailView.layout(0, 0, bmWidth, bmHeight);
detailView.draw(canvas); //null pointer exception here
}
致电代码:
Bitmap exportBitmap = Bitmap.createBitmap(640, 960, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(exportBitmap);
ObservationDetailsGraphicView view = new ObservationDetailsGraphicView(this);
view.draw(canvas);
我没有在logcat中看到任何相关内容,但这里是堆栈的相关部分:
ObservationDetailsGraphicView(SurfaceView).updateWindow(boolean, boolean) line: 473
ObservationDetailsGraphicView(SurfaceView).dispatchDraw(Canvas) line: 350
ObservationDetailsGraphicView(View).draw(Canvas) line: 6883
ObservationDetailsGraphicView(SurfaceView).draw(Canvas) line: 336