Friday 28 October 2016

Android surfaceview oncreated not called when drawing to bitmap

I'm getting a null pointer exception in SurfaceView.updateWindow and I guess its because I'm in onDraw but have never received surfaceCreated on the surface. So is there a way to work around this or to force surfacecreated to get called when rendering directly to bitmap.



This surface view renders correctly when added as a contentview in an activity. But I actually want to render the surfaceview directly to a bitmap without displaying it anywhere.



SurfaceView constructor:



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
}


Calling code:



Bitmap exportBitmap = Bitmap.createBitmap(640, 960, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(exportBitmap);
ObservationDetailsGraphicView view = new ObservationDetailsGraphicView(this);
view.draw(canvas);


I don't see anything relevant in logcat but here is the relevant part of the stack:



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

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...