如何解决加载程序动画空指针?
| 我正在尝试通过以下方式实现动画可绘制对象:<?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/load_0000\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0001\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0002\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0003\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0004\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0005\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0006\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0007\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0008\" android:duration=\"50\" />
<item android:drawable=\"@drawable/load_0009\" android:duration=\"50\" />
</animation-list>
但是,当我执行以下代码时,我会抛出一个空指针。
ImageView img = (ImageView)findViewById(R.id.load_bar);
img.setBackgroundResource(R.drawable.loader_animation); //null here.
为什么? loader_animation是文件名,它位于drawable /
解决方法
问题在于
img
为空:
您已加载ID为load_bar
的setContentView
(或膨胀并添加到contentView中)的xml中是否有一个View?
请注意,仅保留一些XML或以后再加载它是行不通的。 findViewById查找当前视图!
如果尚未加载,请使用inflater
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。