java.lang.IllegalStateException: Underflow in restore - more restores than saves

安稳与你 提交于 2020-01-11 05:25:07

问题


I am using rippleeffect library for my project. But in Android Nougat and Marshmallow, App crashes due to this library:

compile 'com.github.traex.rippleeffect:library:1.3'

The error message is:

FATAL EXCEPTION: main Process: com.test.testapp, PID: 17713 java.lang.IllegalStateException: Underflow in restore - more restores than saves at android.graphics.Canvas.native_restore(Native Method) at android.graphics.Canvas.restore(Canvas.java:522) at com.andexert.library.RippleView.draw(RippleView.java:170) ........................ ........................

As far as the below link this is a known issue. https://github.com/traex/RippleEffect/issues/76 and I also tried so many solutions from stackoverflow but luck did favor so far!!!

What can be done to solve this problem?


回答1:


I faced the same issue and did not found a good solution for that.But If you

  • Downgrade targetSdkVersion to 22 you can run it: meaning it does not crash! but I really don't recommend that.
  • Try to compile it with compile this dependency ->'com.github.emanzanoaxa:RippleEffect:52ea2a0ab6'
  • Call canvas.save(); before every restore() is another suggestion from your link given that you can try
  • You can also try to add that library inside your project and use it

https://codeload.github.com/traex/RippleEffect/zip/master (from the link you provided there are solutions that people have tried use them)


Or i suggest you to create them by yourself no libraries needed at all!

Ripple touch effect was introduced in Android 5.0 (API level 21) and the animation is implemented by the new RippleDrawable class.

General, ripple effect for regular buttons works by default in API 21 and for other touchable views, it can be achieved by specifying:

android:background="?attr/selectItemBackground"

for ripples contained within the view or:

android:background="?attr/selectItemBackgroundBorderless"

for ripples that extend beyond the view's bounds.

You can achieve the same in code using:

int[] attrs = new int[]{R.attr.selectItemBackground};
TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
myView.setBackgroundResource(backgroundResource);

If you want to customize the ripple effect into a view, you need to create a new XML file, inside the drawable directory.

Examples:

Example 1: An unbounded ripple

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ffff0000" />

Example 2: Ripple with mask and background color

<ripple android:color="#7777666"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/mask"
        android:drawable="#ffff00" />
    <item android:drawable="@android:color/white"/>
</ripple>

Example 3: Ripple on top a drawable resource

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ff0000ff">
    <item android:drawable="@drawable/my_drawable" />
</ripple>

How to Use: To attach your ripple xml file to any view, set it as background as following: assuming your ripple file is named my_ripple.xml. in exmple 1, 2 or 3

<View 
    android:id="@+id/myViewId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_ripple" />



回答2:


they fixed the bug :P RippleEffect




回答3:


Check your code again. You probably have an extra .restore() method somewhere.



来源:https://stackoverflow.com/questions/41390367/java-lang-illegalstateexception-underflow-in-restore-more-restores-than-saves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!