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.

posted @   行走的思想  阅读(38)  评论(0编辑  收藏  举报  
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示