underflow in restore in android 4.3

被刻印的时光 ゝ 提交于 2019-12-04 19:32:59

问题


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)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18220084/underflow-in-restore-in-android-4-3

标签

工具导航Map