簡體   English   中英

未找到資源異常將可繪制動畫加載到圖像視圖中

[英]Resources not found exception loading drawable animation into image view

我正在嘗試為我在活動中用作背景的圖像視圖設置動畫,但是在加載活動時應用程序崩潰了。 所有圖像和.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: 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

我會把它放在評論中,但不幸的是我沒有足夠高的代表。 所以我會盡力解決問題。

您提到所有圖像都存在於drawable-hdpi文件夾中。 但是如果你注意到錯誤,它看起來就像是在res/drawable-xxhdpi文件夾中res/drawable-xxhdpi文件。 我建議將.xml文件從-hdpi文件夾復制到-xxhdpi文件夾中,看看它是否有效!

修復!! 由於它根本沒有加載動畫,所以我決定制作一個onWindowFocusedChanged方法,以便在活動加載后加載動畫。 我還將imageview的背景設置為.xml中的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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM