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
    评论
引用\[1\]和\[2\]提供了关于"java.lang.IllegalStateException: Cannot create a session after the response has been committed"错误的解释。这个错误是因为在创建session之前,已经将response进行了提交。也就是说,在response提交之后不能再操作session。解决这个问题的方法是使用SessionStatus来销毁session数据,而不是使用request.getSession().invalidate()方法。具体的解决方法可以参考引用\[2\]中的示例代码。引用\[3\]提供了另一种情况下出现这个错误的示例代码,即在存入session时报错,说明响应流已经关闭。 #### 引用[.reference_title] - *1* [java.lang.IllegalStateException: Cannot create a session after the response has been committed](https://blog.csdn.net/u013485584/article/details/95597992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [出现java.lang.IllegalStateException: Cannot create a session after the response has been committed...](https://blog.csdn.net/zha6476003/article/details/108771735)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Caused by: java.lang.IllegalStateException: Cannot create a session after the response has been comm](https://blog.csdn.net/m0_51953797/article/details/126302139)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值