2

我有一个按钮,单击时应该显示动画内容。单击时出现非法状态异常。它说“无法在分离视图上启动此动画师!”。不知道是什么原因造成的。我提到了类似的问题,但没有帮不了我。有人可以帮我吗?

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"
/>

代码:

 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();

例外:

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)
4

1 回答 1

1

问题是,你view没有附加任何东西,它既不是任何活动的一部分,也不是与屏幕布局相关的任何东西

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

所以因此问题,您可以选择做

 youAlertDiaogInstance.setContentView(view);

根据你的评论

没有alertdialog。我只是给了布局名称alert_lyt.xml:

然后使用

view=findViewById(R.layout.container);

其中 container 是根布局的 id 为

<?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>
于 2018-05-01T17:28:29.063 回答