java - Canvas object must be the same instance that was previously returned by lockCanvas -
i have custom surfaceview method dodraw() draws background , bitmap. problem when run error
caused by: java.lang.illegalargumentexception: canvas object must same instance returned lockcanvas
i don't see why happening. don't declare other canvases anywhere else in code. have 2 other classes, mainactivity , surfaceviewexample. mainactivity has intent open surfaceviewexample, , surfaceviewexample has method called buttons.
ourview class:
package com.thatoneprogrammerkid.gameminimum; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.util.attributeset; import android.view.surfaceholder; import android.view.surfaceview; public class ourview extends surfaceview implements surfaceholder.callback { private surfaceholder holder; private bitmap testimg; public int xcoord = 500; public int ycoord = 500; public ourview(context context) { super(context); init(); } public ourview(context context, attributeset attrs) { super(context, attrs); init(); } public ourview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } private void init() { holder = getholder(); holder.addcallback(this); testimg = bitmapfactory.decoderesource(getresources(),r.drawable.testimg); } void moveimage(int xchange, int ychange) { xcoord += xchange; ycoord += ychange; dodraw(); } void dodraw() { canvas mycanvas = holder.lockcanvas(); if (mycanvas != null) { mycanvas.drawargb(255, 55, 255, 255); mycanvas.drawbitmap(testimg, xcoord, ycoord, null); } holder.unlockcanvasandpost(mycanvas); } @override public void surfacecreated(final surfaceholder holder) { dodraw(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) {} @override public void surfacedestroyed(surfaceholder holder) {} }
move unlockcanvasandpost()
inside if (mycanvas != null) {
statement. guess lockcanvas()
returning null, , you're attempting unlock null reference.
looking @ the source code, test "is same" comes before test "is locked @ all" -- , mcanvas
initialized non-null value.
Comments
Post a Comment