Android AnimationDrawable在getBackground()上获取null
我正在尝试使用以下代码制作示例程序的动画:Android AnimationDrawable在getBackground()上获取null,android,null,animationdrawable,Android,Null,Animationdrawable,我正在尝试使用以下代码制作示例程序的动画: AnimationDrawable animation; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading); Image
AnimationDrawable animation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading);
ImageView animatedImage = (ImageView) findViewById(R.id.animation);
animatedImage.setBackgroundResource(R.drawable.animate_bag);
animation = (AnimationDrawable) animatedImage.getBackground();
}
public void startAnimate(View v)
{
if (animation != null)
animation.start();
} //eof OnClick
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate"
android:onClick="startAnimate" />
<ImageView
android:id="@+id/animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animate_bag" />
</LinearLayout>
XML文件是:
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate"
android:onClick="startAnimate" />
<ImageView
android:id="@+id/animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animate_bag" />
</LinearLayout>
我遇到的问题是animateImage.getBackground()返回null
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate"
android:onClick="startAnimate" />
<ImageView
android:id="@+id/animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animate_bag" />
</LinearLayout>
非常感谢您的提示:-)
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate"
android:onClick="startAnimate" />
<ImageView
android:id="@+id/animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animate_bag" />
</LinearLayout>
谢谢,
西蒙(希望这对你有帮助;)
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate"
android:onClick="startAnimate" />
<ImageView
android:id="@+id/animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animate_bag" />
</LinearLayout>
如果getBackground()返回null,请使用getDrawable()方法
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate"
android:onClick="startAnimate" />
<ImageView
android:id="@+id/animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animate_bag" />
</LinearLayout>
嗨,我知道这很旧,但你找到解决办法了吗?谢谢