animation-list AnimationDrawable null 空指针 原因


在 res/drawable 目录下 有个voice_to_icon.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/chatto_voice_playing_f1"
        android:duration="200" />
    <item
        android:drawable="@drawable/chatto_voice_playing_f2"
        android:duration="200" />
    <item
        android:drawable="@drawable/chatto_voice_playing_f3"
        android:duration="200" />
</animation-list>


我是这样引用:

	private void showAnimation() {
		// play voice, and start animation
		if (message.direct == EMMessage.Direct.RECEIVE) {
			voiceIconView.setImageResource(R.drawable.voice_from_icon);
		} else {
			voiceIconView.setImageResource(R.drawable.voice_to_icon);
		}
		voiceAnimation = (AnimationDrawable) voiceIconView.getDrawable();
		if (voiceAnimation != null)
			voiceAnimation.start();
	}

在执行到  voiceAnimation = (AnimationDrawable) voiceIconView .getDrawable(); 时候,报了一个空指针异常


原因是 :

@drawable/chatto_voice_playing_f1
被我放在了 res/drawable-land-hdpi 目录下

总结 :
phone开发resource放在 pad resource中会出现空指针异常,放资源的时候,得慎重注意这一状况

本小计完结了

 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
animation-list是Android中的一个Drawable资源类型,用于定义逐帧动画。它可以让你定义一系列不同的Drawable资源,并通过设置每个Drawable资源在动画中的时长和顺序来创建一个动画序列。每个Drawable资源可以是一个静态图像、一个逐帧动画或一个过渡动画。你可以将animation-list应用于任何View或ViewGroup的背景,以创建动态的背景效果。 要使用animation-list,你需要在res/drawable文件夹下创建一个XML文件,并在其中定义Drawable资源。以下是一个简单的animation-list示例: ```xml <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/frame1" android:duration="100" /> <item android:drawable="@drawable/frame2" android:duration="100" /> <item android:drawable="@drawable/frame3" android:duration="100" /> </animation-list> ``` 在这个例子中,我们定义了一个animation-list,其中包含三个Drawable资源:frame1、frame2和frame3。我们设置了每个Drawable资源在动画序列中的时长为100毫秒,并将oneshot属性设置为true,表示动画只会播放一次。要在代码中使用这个animation-list,可以通过以下方式: ```java ImageView imageView = findViewById(R.id.image_view); imageView.setBackgroundResource(R.drawable.my_animation); AnimationDrawable anim = (AnimationDrawable) imageView.getBackground(); anim.start(); ``` 在这个例子中,我们首先获取了一个ImageView,并将其背景设置为animation-list资源my_animation。然后我们获取ImageView的背景,并将其转换为AnimationDrawable对象。最后,我们调用start()方法开始动画。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值