1

Iam trying to have an animation inside a dialog box so it would look like the animation is in front of the activity and be gone after 2 sec, but it always crash and only show the layout, not the animation, here are my codes:

Method in my 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();
}

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

anim/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) at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1260) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:153) at android.app.ActivityThread.main(ActivityThread.java:5071) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method)

Line 205: animcon.start();

Suhas Bachewar
  • 1,234
  • 7
  • 20
Ralph
  • 510
  • 1
  • 10
  • 19

5 Answers5

0

You have to extends Dialog in your ImageView.so Change

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

into

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

and also

Read more details getDrawable() deprecated API 22

Community
  • 1
  • 1
sasikumar
  • 10,919
  • 2
  • 21
  • 42
  • getResources().getDrawable() deprecated API 22 refer http://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22 – sasikumar Feb 17 '16 at 07:16
0

Try this

dialog.setContentView(R.layout.connected);
    ImageView IVcon = (ImageView)dialog.findViewById(R.id.IVcon);
Nivedh
  • 931
  • 1
  • 7
  • 19
0

getDrawable() is returning null. You can only use it to get a drawable if you previously assigned a drawable to it. Unfortunately, you only assigned a background drawable, which is not the same thing. Try using getBackground() instead.

I'm not sure you can simply call start() on a background drawable, though.

Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302
0

use dialog.setContentView instead of just setContentView

Move anim/connected.xml to drawable/connected.xml and leave it as it is.

Then in your connectedAnim(), instead of

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

use

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

and use this

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

instead of

AnimationDrawable animcon = (AnimationDrawable)IVcon.getDrawable();
kaaloraat
  • 421
  • 3
  • 8
  • move it to the drawable folder? or put the codes of anim/connected to layout/connected? – Ralph Feb 17 '16 at 07:28
  • just cut the `connected.xml` from `anim` directory and paste it to `drawable` directory. it is because we want to set a drawable as the background resource fro the `IVcon` – kaaloraat Feb 17 '16 at 07:31
  • so the problem is i shouldnt set a background of anim cause .getDrawable() cant get it? and only drawables can be get by getBackground()? – Ralph Feb 17 '16 at 07:41
  • you are right. Before, you weren't able to get the background using `getDrawable()` beacuse you were not setting one to `IVcon`. – kaaloraat Feb 17 '16 at 07:43
0

First move your connected.xml from anim to drawable folder. other changes I have commented in side code.

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();
}
Suhas Bachewar
  • 1,234
  • 7
  • 20
  • thank you! i already did this and it's working, you got the same answer as the other one at the top – Ralph Feb 17 '16 at 07:58