我需要帮助。上面写着:错误:找不到符号fruitImage.setBackgroundResource(R.drawable.myfruits(;^符号:变量myfruits位置:可分级提取
----------------------这是代码------------------
myfruits.xml (located at drawable)
<?xmlversion="1.0"encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/img_grapes" android:duration="200"/>
<item android:drawable="@drawable/img_lemon" android:duration="200"/>
<item android:drawable="@drawable/img_orange" android:duration="200"/>
<item android:drawable="@drawable/img_pear" android:duration="200"/>
<item android:drawable="@drawable/img_strawberry" android:duration="200"/>
</animation-list>
---------------主要活动------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView fruitImage = (ImageView) findViewById(R.id.imgFruit);
fruitImage.setBackgroundResource(R.drawable.myfruits);
fruitAnimation = (AnimationDrawable) fruitImage.getBackground();
}
以下是解释。
https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable
在您的代码中,您必须更改myfruits.xml.
<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/img_grapes" android:duration="200"/>
<item android:drawable="@drawable/img_lemon" android:duration="200"/>
<item android:drawable="@drawable/img_orange" android:duration="200"/>
<item android:drawable="@drawable/img_pear" android:duration="200"/>
<item android:drawable="@drawable/img_strawberry" android:duration="200"/>
</animation-list>
-----主要活动——
ImageView fruitImage = (ImageView) findViewById(R.id.imgFruit);
fruitImage.setBackgroundResource(R.drawable.myfruits);
AnimationDrawable fruitAnimation = (AnimationDrawable) fruitImage.getBackground();
fruitAnimation.start();