2011-09-27 41 views
0

我试图让这个例子的工作: http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html的Android,从文档做的AnimationDrawable ...不工作:(

但因为它启动时,它崩溃(强制关闭),我真的不知道问题出在哪里,因为我是新来这个

我的代码: (.java文件)

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

动画XML文件

<?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文件

<?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> 

注意,动画XML文件在res /提拉 - 华电国际,把它在res /动画文件夹是给我的错误。另外请注意,我是Android noobie;)

谢谢!
Ryan

编辑;

有在演示代码中的错误:http://code.google.com/p/android/issues/detail?id=1818 所以我修改了代码:pastebin.com/ZtLf8J87 和它的作品;)

回答

1

动画XML应在res /动画总是决定。它抛出一个错误的原因是由于您使用的ImageView IMG =

(ImageView的)findViewById(R.id.spinning_wheel_image);

之前设置的内容视图即

的setContentView(R.layout.main);

用于跟踪错误和调试使用Logcat.You可能会在Eclipse中找到以下位置。 Windows => Open Perspective => Logcat

+0

好的,做了你说的,我得到了错误“spin_animation无法解析或不是字段”在java文件中的“img.setBackgroundResource(R.drawable.spin_animation) ;” – Ryan

+0

你确定res/drawable文件夹中有一个可绘制的spin_animation? –

+0

我认为是这样,它看起来如此:http://free-image-host.ezee.se/pic.php?p=12709110_capture2.jpg – Ryan