在对话框中

时间:2016-02-17 06:57:52

标签: android android-animation android-dialog

Iam试图在dialog框内放置一个动画,这样看起来动画就在活动前面,并在2秒后消失,但它总是崩溃,只显示layout ,而不是动画,这是我的代码:

我的MainActivity.java中的方法

public void connectedAnim(){
    Dialog dialog = new Dialog(MainActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.connected);
    dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

    IVcon = (ImageView)findViewById(R.id.IVcon);
    IVcon.setBackgroundResource(R.anim.connected);

    final AnimationDrawable animcon = (AnimationDrawable)IVcon.getDrawable();
    dialog.setCancelable(true);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            animcon.start();
        }
    });
    dialog.show();
}

布局/ connected.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:id="@+id/IVcon"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

动画/ connected.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/connected" android:duration="500"/>
    <item android:drawable="@drawable/disconnected" android:duration="500"/>

</animation-list>

logcat的

  

java.lang.NullPointerExceptionat   com.ardudroid.sample.bluetoothswitch.MainActivity $ 7.onShow(MainActivity.java:205)                                                                                           在android.app.Dialog $ ListenersHandler.handleMessage(Dialog.java:1260)                                                                                           在android.os.Handler.dispatchMessage(Handler.java:99)                                                                                           在android.os.Looper.loop(Looper.java:153)                                                                                           在android.app.ActivityThread.main(ActivityThread.java:5071)                                                                                           at java.lang.reflect.Method.invokeNative(Native Method)                                                                                           在java.lang.reflect.Method.invoke(Method.java:511)                                                                                           在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)                                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)                                                                                           在dalvik.system.NativeStart.main(本地方法)

     

第205行:animcon.start();

5 个答案:

答案 0 :(得分:0)

您必须在ImageView.so更改

中扩展Dialog
  IVcon = (ImageView)findViewById(R.id.IVcon); 

进入

  IVcon = (ImageView) dialog.findViewById(R.id.IVcon);  

以及

了解详情getDrawable() deprecated API 22

答案 1 :(得分:0)

试试这个

dialog.setContentView(R.layout.connected);
    ImageView IVcon = (ImageView)dialog.findViewById(R.id.IVcon);

答案 2 :(得分:0)

getDrawable()返回null。如果您之前已为其指​​定了drawable,则只能使用它来获取drawable。不幸的是,你只分配了一个背景 drawable,这不是一回事。请尝试使用getBackground()

我不确定你是否可以简单地在背景drawable上调用start()。

答案 3 :(得分:0)

使用dialog.setContentView而非setContentView

anim/connected.xml移至drawable/connected.xml并保持原样。

然后在你的connectedAnim()中,而不是

IVcon = (ImageView)dialog.findViewById(R.id.IVcon);
IVcon.setBackgroundResource(R.anim.connected); 

使用

IVcon = (ImageView)dialog.findViewById(R.id.IVcon);
IVcon.setBackgroundResource(R.drawable.connected);  

并使用此

AnimationDrawable animcon = (AnimationDrawable) IVcon.getBackground();  

而不是

AnimationDrawable animcon = (AnimationDrawable)IVcon.getDrawable();

答案 4 :(得分:0)

首先将您的 connected.xml 动画移至 drawable 文件夹。 我在旁边代码中评论过的其他更改。

public void connectedAnim(){
       Dialog dialog = new Dialog(MainActivity.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.connected); // change to dialog.setContentView
            dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

            IVcon = (ImageView)dialog.findViewById(R.id.IVcon);

            IVcon.setBackgroundResource(R.drawable.connected);// Drawable file instead of anim move same file from anim folder to Drawable before use..

            final AnimationDrawable animcon = (AnimationDrawable) IVcon.getBackground(); // instead of getDrawable() use this
            dialog.setCancelable(true);

            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    animcon.start();
                }
            });
            dialog.show();
}