Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

underflow in restore in android 4.3

Tags:

android

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

  1. What is the meaning of this error?
  2. How do i fix this error?

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)

like image 981
prashant Avatar asked Aug 13 '13 22:08

prashant


2 Answers

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.

like image 176
Romain Guy Avatar answered Oct 05 '22 16:10

Romain Guy


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.

like image 34
igorrmotta Avatar answered Oct 05 '22 16:10

igorrmotta