加载可绘制动画到图片视图时出现资源未找到异常

3
我是一名有用的助手,可以为您翻译文本。以下是需要翻译的内容:

我正在尝试为我的活动设置一个作为背景的图像视图的动画,但应用在加载活动时会崩溃。所有的图像和.xml文件都在drawable-hdpi文件夹中,所以我不确定为什么它找不到。请告诉我是否有更好的方法将动画设置为活动背景。

这是我的动画.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/bow1" android:duration="60" />
<item android:drawable="@drawable/bow2" android:duration="60" />
<item android:drawable="@drawable/bow3" android:duration="60" />
<item android:drawable="@drawable/bow4" android:duration="60" />
<item android:drawable="@drawable/bow5" android:duration="60" />
<item android:drawable="@drawable/bow6" android:duration="60" />
<item android:drawable="@drawable/bow7" android:duration="60" />
<item android:drawable="@drawable/bow8" android:duration="60" />
<item android:drawable="@drawable/bow9" android:duration="60" />
</animation-list>

这是我尝试运行它的代码:

这是我正在使用的代码:

public class MainActivity extends Activity {

ImageView bground;
AnimationDrawable bganim;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(R.layout.activity_main);

    bground = (ImageView)findViewById(R.id.background);
    bground.setBackgroundResource(R.drawable.bowanimbg);
    bganim = (AnimationDrawable) bground.getBackground();
    bganim.start();
}

java.lang.RuntimeException:无法启动组件Activity ComponentInfo{com.example.lucidity/com.example.lucidity.MainActivity}:android.content.res.Resources$NotFoundException:从可绘制资源ID#0x7f02000b加载文件res/drawable-xxhdpi/bowanimbg.xml


请仔细检查拼写。 - Aniruddha
我已经检查了多次,所有内容都是正确的。 - MDubzem
2个回答

1
我会把这个放在评论中,但不幸的是我的声望不够高。所以我将尽力解决问题。
你提到所有的图像都在drawable-hdpi文件夹中。但如果你注意到错误,它似乎正在搜索res/drawable-xxhdpi文件夹中的文件。我建议将.xml文件从-hdpi文件夹复制到-xxhdpi文件夹中,看看是否有效!

-1

问题已经解决了!由于动画根本没有加载,所以我决定创建一个onWindowFocusedChanged方法,这样一旦活动被加载,它就会加载动画。我还在.xml中将imageview的背景设置为我的bowanimbg.xml文件,最初我认为这不是必要的。

public void onWindowFocusChanged(boolean hasFocus) {
bground = (ImageView)findViewById(R.id.background);
bground.setImageResource(R.drawable.bowanimbg);
bganim = (AnimationDrawable) bground.getBackground();
if (hasFocus) {
    bganim.start();
} 
else {
    bganim.stop();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接