I'm getting a IllegalStateException: underflow in restore exception, which is causing my application to crash. This started happening after android 4.3 update. On android 3.0 - 4.2.x it works fine.
The exception happens on the second
canvas.restore();
Given below is my drawing code
private void doDraw(Canvas canvas) {
if(mTickerBackGround!=null && (!mTickerBackGround.isRecycled())){
canvas.drawBitmap(mTickerBackGround, 0, 0, null);
}
if((mBitMapBuffer!=null)){
canvas.save();
canvas.translate(mX, 0);
if(!mBitMapBuffer.isRecycled()){
canvas.drawBitmap(mBitMapBuffer, 0, 0, null);
}
canvas.restore();
if(bitMapWidth+mX<mCanvasWidth){
canvas.translate(bitMapWidth+mX, 0);
if(!mBitMapBuffer.isRecycled()){
canvas.drawBitmap(mBitMapBuffer, 0, 0, null);
}
canvas.restore();
}
if(bitMapWidth+mX<=0){
mX = 0;
}else if(Math.abs(mX)>(bitMapWidth)){
mX= mCanvasWidth;
}
mX-=TickerConstants.SCROLLING_SMOOTHNESS*density;;
}
if(mLogo!=null && (!mLogo.isRecycled())){
canvas.drawBitmap(mLogo, mCanvasWidth-(60*density), mLogo.getHeight()/6, null);
}
}
My question is
Stack trace message generated is . Line 165 corresponds to second canvas.restore()
08-13 18:13:09.083: E/AndroidRuntime(14139): FATAL EXCEPTION: Thread-506 08-13 18:13:09.083: E/AndroidRuntime(14139): java.lang.IllegalStateException: Underflow in restore 08-13 18:13:09.083: E/AndroidRuntime(14139): at android.graphics.Canvas.restore(Native Method) 08-13 18:13:09.083: E/AndroidRuntime(14139): at com.my.package.name.ticker.TickerSurfaceView$TickerThread.doDraw(TickerSurfaceView.java:165) 08-13 18:13:09.083: E/AndroidRuntime(14139): at com.my.package.name.ticker.TickerSurfaceView$TickerThread.run(TickerSurfaceView.java:128)
The bug is in this section of your code:
if(bitMapWidth+mX<mCanvasWidth){
canvas.translate(bitMapWidth+mX, 0);
if(!mBitMapBuffer.isRecycled()){
canvas.drawBitmap(mBitMapBuffer, 0, 0, null);
}
canvas.restore();
}
You are calling restore()
without calling save()
first. You don't even need that call to translate()
either, you could just pass the x and y coordinates to the drawBitmap()
call.
public void restore ()
Added in API level 1 This call balances a previous call to save(), and is used to remove all modifications to the matrix/clip state since the last save call. It is an error to call restore() more times than save() was called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With