I am trying to get this example working:
http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
but as it starts, it crashes (force closes) and I am not really sure where the problem is as I am new to this.
My code:
(Java file)
package com.ryan.test;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class TestAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
setContentView(R.layout.main);
}
}
Animation XML file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/note0" android:duration="50" />
<item android:drawable="@drawable/note1" android:duration="50" />
</animation-list>
Main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/note0"
android:id="@+id/spinning_wheel_image"></ImageView>
</LinearLayout>
Note that the animation xml file is in res/drawable-hdpi, putting it in the res/anim folder was giving me errors. Also note I am an Android noobie 😉
Thanks!
Ryan
EDIT;
There IS a bug in the demo code: http://code.google.com/p/android/issues/detail?id=1818
so i modified the code: pastebin.com/ZtLf8J87
and it works 😉
The animation xml should always decide in res/anim. The reason it is throwing an error is due to the fact that you are using ImageView img =
before setting the content view i.e
For tracking the errors and debugging use Logcat.You may find that in Eclipse at following location. Windows => Open Perspective => Logcat