android 动画问题,在线等!
先贴出错误log
01-09 01:35:17.295: E/AndroidRuntime(2323): FATAL EXCEPTION: main
01-09 01:35:17.295: E/AndroidRuntime(2323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.animationforexample2/com.example.animationforexample2.MainActivity}: android.content.res.Resources$NotFoundException: File res/anim/money_anim.xml from drawable resource ID #0x7f040000
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.os.Looper.loop(Looper.java:137)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-09 01:35:17.295: E/AndroidRuntime(2323): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 01:35:17.295: E/AndroidRuntime(2323): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 01:35:17.295: E/AndroidRuntime(2323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-09 01:35:17.295: E/AndroidRuntime(2323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-09 01:35:17.295: E/AndroidRuntime(2323): at dalvik.system.NativeStart.main(Native Method)
01-09 01:35:17.295: E/AndroidRuntime(2323): Caused by: android.content.res.Resources$NotFoundException: File res/anim/money_anim.xml from drawable resource ID #0x7f040000
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.content.res.Resources.loadDrawable(Resources.java:1953)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.content.res.Resources.getDrawable(Resources.java:660)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.view.View.setBackgroundResource(View.java:14468)
01-09 01:35:17.295: E/AndroidRuntime(2323): at com.example.animationforexample2.MainActivity.init(MainActivity.java:29)
01-09 01:35:17.295: E/AndroidRuntime(2323): at com.example.animationforexample2.MainActivity.onCreate(MainActivity.java:20)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.app.Activity.performCreate(Activity.java:5104)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-09 01:35:17.295: E/AndroidRuntime(2323): ... 11 more
01-09 01:35:17.295: E/AndroidRuntime(2323): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6: <item> tag requires a 'duration' attribute
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:271)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
01-09 01:35:17.295: E/AndroidRuntime(2323): at android.content.res.Resources.loadDrawable(Resources.java:1950)
01-09 01:35:17.295: E/AndroidRuntime(2323): ... 18 more
下面是源碼:
MainActivity:
package com.example.animationforexample2;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private AnimationDrawable frameanim;
private Button start,stop;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
imageView=(ImageView) findViewById(R.id.imageView);
start=(Button) findViewById(R.id.start);
stop=(Button) findViewById(R.id.stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);
imageView.setBackgroundResource((int)R.anim.money_anim);
frameanim=(AnimationDrawable) imageView.getBackground();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.start:
frameanim.start();
break;
case R.id.stop:
frameanim.stop();
break;
}
}
}
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:orientation="horizontal" >
<Button
android:id="@+id/start"
android:layout_width="100px"
android:layout_height="fill_parent"
android:text="播放动画" >
</Button>
<Button
android:id="@+id/stop"
android:layout_width="100px"
android:layout_height="fill_parent"
android:text="停止动画" >
</Button>
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="300px"
android:layout_height="300px"
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp"
android:background="@drawable/anim_one"
>
</ImageView>
</LinearLayout>
money_anim:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://shemas.android.com/apk/res/android"
android:oneshot="true"
>
<item android:drawable="@drawable/anim_one" android:duration="300"></item>
<item android:drawable="@drawable/anim_two" android:duration="300"></item>
<item android:drawable="@drawable/anim_three" android:duration="300"></item>
<item android:drawable="@drawable/anim_four" android:duration="300"></item>
<item android:drawable="@drawable/anim_five" android:duration="300"></item>
<item android:drawable="@drawable/anim_six" android:duration="300"></item>
<item android:drawable="@drawable/anim_seven" android:duration="300"></item>
<item android:drawable="@drawable/anim_eight" android:duration="300"></item>
</animation-list>
文件结构:
小弟已经被折磨死了,求大神解答!
[解决办法]
1.把工程clean一下,再重新跑。
2.如果有jar包的话,看看jar包是否有重名冲突的资源。
3.看看你anmi的xml文件中是否有红叉@!
[解决办法]
第四点,看看你引入的那个R
是安卓自带的那个R
但是对应你包名的那个R
[解决办法]
异常里面不是写了嘛,animation定义里面缺少duration属性,
01-09 01:35:17.295: E/AndroidRuntime(2323): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6: <item> tag requires a 'duration' attribute