Android java.lang.IllegalStateException: Cannot start this animator on a detached view!


http://stackoverflow.com/questions/26475147/error-while-trying-to-create-circular-reveal-illegalstateexception-cannot-star

So, I'm experimenting trying to create a circular reveal on API level 21 on a TextView but I keep getting this error. At first I thought it had something to do with the lifecycle of the fragment I was attempting it but then I just tried the same thing in an activity and it still wouldn't work.
Here's the code:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window w = getWindow();
        w.setFlags(
                WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
                WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        w.setStatusBarColor(Color.parseColor("#0277bd"));


        setContentView(R.layout.activity_main);


        TextView tv = (TextView) findViewById(R.id.text);



        int a = (tv.getLeft() + tv.getRight()) / 2;
        int b = (tv.getTop() + tv.getBottom()) / 2;

        int radius = tv.getWidth();

        Animator anim = ViewAnimationUtils.createCircularReveal(tv, a, b, 0, radius);

        anim.start();





    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return super.onCreateOptionsMenu(menu);
    }
}

It's still early days so I can't really find any answers about this. Any ideas?

 


1.
You can use Runnable to do the animation. The Runnable will run after the view's creation. No need to post delay.

view.post(new Runnable()
{ 
       @Override 
       public void run(){ 
           //create your anim here
       } 
});

2.
Or you can do this.
 tv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            Animator anim = ViewAnimationUtils.createCircularReveal(tv, a, b, 0, radius);
            anim.start();
        }
    });

Adding GlobalLayoutListener to wait for the view to be inflated worked for me.

3.
Or you can do this.
tv.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        //start your animation here                        
                    }
                }, 1000);


4.
Or you can do this.

Call your Animator logic from onResume(). That way you'll be sure all views have been attached in the layout.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值