jobin
jobin

Reputation: 1517

CircularReveal IlegalStateException

I have a button which when clicked should do the reveal animation stuff.I get the Illegal State Exception when clicked.It says "Cannot start this animator on a detached view!".No idea what caused this.I referred to similar issues but didn't help me. Could anyone please help me?

alert_lyt.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="A1"
android:textSize="30sp"
android:gravity="center"
android:textColor="#ffffff"
android:id="@+id/button"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="A1"
android:textSize="30sp"
android:gravity="center"
android:textColor="#ffffff"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="A1"
android:textColor="#ffffff"
android:textSize="30sp"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="A1"
android:textColor="#ffffff"
android:textSize="30sp"
android:gravity="center"
/>

CODE:

 initialRadius=0;    
 view=LayoutInflater.from(getApplicationContext()).
 inflate(R.layout.alert_lyt,null);
 centerX=button.getWidth();
 centerY=button.getHeight();
 finalRadius= (int) Math.hypot(view.getWidth(),view.getHeight()); 
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
 {                         
 Animator animator=ViewAnimationUtils.
 createCircularReveal(view,centerX,centerY,initialRadius,finalRadius);
 animator.setDuration(1000);
 animator.start();

Exception:

java.lang.IllegalStateException: Cannot start this animator on a detached view!
        at android.view.RenderNode.addAnimator(RenderNode.java:863)
        at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:300)
        at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:282)
        at android.animation.RevealAnimator.<init>(RevealAnimator.java:37)
        at android.view.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:55)
        at com.globemaster.systemwindows.Splash$1.onClick(Splash.java:51)
        at android.view.View.performClick(View.java:5716)
        at android.widget.TextView.performClick(TextView.java:10926)
        at android.view.View$PerformClick.run(View.java:22596)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Upvotes: 2

Views: 186

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

The issue is, your view is not attached with anything neither it is a part of any activity or anything related to screen layout

// independent of any layout
view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.alert_lyt,null);

so hence the issue , optionally you can do

 youAlertDiaogInstance.setContentView(view);

as per your comment

There is no alertdialog.I just gave the layout name alert_lyt.xml:

then use

view=findViewById(R.layout.container);

where container is the id of root layout as

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#000000"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">... </LinearLayout>

Upvotes: 1

Related Questions