我有一个按钮,当点击时应该显示动画的东西。点击时我得到非法状态异常。它说"无法在独立视图上启动此动画师!&# 34;。不知道是什么导致了这个。我提到了类似的问题,但没有帮助我。有人可以帮帮我吗?
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();
例外:
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)
答案 0 :(得分:1)
问题是,您的view
没有附加任何活动的任何内容或任何与屏幕布局相关的内容
// independent of any layout
view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.alert_lyt,null);
因此问题,你可以选择
youAlertDiaogInstance.setContentView(view);
根据您的评论
There is no alertdialog.I just gave the layout name 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>