2

我在 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   
4

0 回答 0