我的应用程序收到一些崩溃报告,如下所示:
java.lang.IllegalStateException:
at android.view.RenderNode.addAnimator (RenderNode.java:817)
at android.view.RenderNodeAnimator.setTarget (RenderNodeAnimator.java:277)
at android.view.RenderNodeAnimator.setTarget (RenderNodeAnimator.java:261)
at android.animation.RevealAnimator.<init> (RevealAnimator.java:37)
at android.view.ViewAnimationUtils.createCircularReveal (ViewAnimationUtils.java:48)
at com.example.myapp.MyConfigureActivity$8.run (MyConfigureActivity.java)
at android.os.Handler.handleCallback (Handler.java:739)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:211)
at android.app.ActivityThread.main (ActivityThread.java:5335)
at java.lang.reflect.Method.invoke (Method.java)
at java.lang.reflect.Method.invoke (Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:811)
我在
Activity
中使用以下函数使视图可见(使用动画)或使视图消失(再次使用动画):public class MyConfigureActivity extends AppCompatActivity {
static private Integer lastXup;
static private Integer lastYup;
void animateView(final View view, final int visibility) {
final int duration = 250;
view.post(new Runnable()
{
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void run()
{
// get the center for the clipping circle
int w = view.getWidth();
int h = view.getHeight();
int cx = (lastXup == null) ? w/2 : lastXup;
int cy = (lastYup == null) ? h/2 : 0;
// get the final radius for the clipping circle
float r = Math.max(cx, w-cx);
Animator anim;
if (visibility == View.VISIBLE) {
// create the animator for this view (the start radius is zero)
anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, r);
// make the view visible before starting the animation
view.setVisibility(View.VISIBLE);
} else {
// create the animation (the final radius is zero)
anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, r, 0);
// make the view GONE when the animation is finished
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});
}
anim.setDuration(duration);
anim.start();
}
});
}
// https://stackoverflow.com/a/11001443/4070848
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_UP:
lastXup = (int) event.getRawX();
lastYup = (int) event.getRawY();
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
}
在绝大多数设备上,这似乎工作得很好,而且我从未能够重现崩溃,但仍有大量的崩溃报告。知道是什么原因吗?
最佳答案
查看RenderNode
的源代码,您可以找到在addAnimator
中引发异常的原因:
if (mOwningView == null || mOwningView.mAttachInfo == null) {
throw new IllegalStateException("Cannot start this animator on a detached view!");
}
因此,当您在应用程序关闭和/或当前活动关闭时启动动画,或者由于另一个原因删除视图时,将出现错误-添加对案例的检查(例如在
onPause
中设置一些变量isanimationallowed=false,在onResume
中设置isanimationallowed=true)。关于android - Android:与createCircularReveal有关的IllegalStateException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46111801/