资源没有发现异常加载可绘制动画到图像视图。

时间:2022-01-21 23:05:59

I'm trying to animate an image view that I'm using as a background in my activity but the app crashes when loading the activity. All the images and the .xml file are in the drawable-hdpi folder so I'm not sure why it's not finding it. Please let me know if there is a better way to set an animation as an activity background.

我正在尝试给一个图像视图做动画,在我的活动中使用它作为背景,但在加载活动时应用程序崩溃。所有的图像和.xml文件都在drawable-hdpi文件夹中,所以我不确定为什么没有找到它。如果有更好的方法来设置动画作为活动背景,请让我知道。

Heres my .xml file for the animation:

这是我的。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>

Heres the code I'm using to try run it:

这是我尝试运行的代码:

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: Unable to start activity ComponentInfo{com.example.lucidity/com.example.lucidity.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable-xxhdpi/bowanimbg.xml from drawable resource ID #0x7f02000b

. lang。RuntimeException:无法启动活动ComponentInfo{com.example.lucidity/com.example.lucidity。MainActivity }:android.content.res。资源$ NotFoundException:文件res / drawable-xxhdpi / bowanimbg。来自drawable resource ID #0x7f02000b的xml。

2 个解决方案

#1


1  

I would put this in a comment, but unfortunately I don't have a high enough rep. So I'll do my best to solve the problem.

我会在评论中说,但遗憾的是我没有足够高的代表,所以我会尽我最大的努力去解决这个问题。

You mentioned that all of the images were present in the drawable-hdpi folder. But if you notice the error, it looks like it is searching for the file in the res/drawable-xxhdpi folder. What I would suggest is copying the .xml files from the -hdpi folder into the -xxhdpi folder and seeing if it works!

您提到所有的图像都存在于drawable-hdpi文件夹中。但是如果您注意到这个错误,它看起来就像是在res/drawable-xxhdpi文件夹中搜索文件。我建议将.xml文件从-hdpi文件夹中复制到-xxhdpi文件夹中,看看是否有效!

#2


-1  

Fixed it!! Since it wasn't loading the animation at all I decided to make an onWindowFocusedChanged method so that it would load the animation once the activity had loaded. I also set the background of the imageview to my bowanimbg.xml file in the .xml which I initially thought wouldn't be necessary.

固定它! !因为它没有加载动画,所以我决定做一个onWindowFocusedChanged方法,以便在活动加载后加载动画。我还将imageview的背景设置为我的bowanimbg。xml文件在.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();
}

#1


1  

I would put this in a comment, but unfortunately I don't have a high enough rep. So I'll do my best to solve the problem.

我会在评论中说,但遗憾的是我没有足够高的代表,所以我会尽我最大的努力去解决这个问题。

You mentioned that all of the images were present in the drawable-hdpi folder. But if you notice the error, it looks like it is searching for the file in the res/drawable-xxhdpi folder. What I would suggest is copying the .xml files from the -hdpi folder into the -xxhdpi folder and seeing if it works!

您提到所有的图像都存在于drawable-hdpi文件夹中。但是如果您注意到这个错误,它看起来就像是在res/drawable-xxhdpi文件夹中搜索文件。我建议将.xml文件从-hdpi文件夹中复制到-xxhdpi文件夹中,看看是否有效!

#2


-1  

Fixed it!! Since it wasn't loading the animation at all I decided to make an onWindowFocusedChanged method so that it would load the animation once the activity had loaded. I also set the background of the imageview to my bowanimbg.xml file in the .xml which I initially thought wouldn't be necessary.

固定它! !因为它没有加载动画,所以我决定做一个onWindowFocusedChanged方法,以便在活动加载后加载动画。我还将imageview的背景设置为我的bowanimbg。xml文件在.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();
}