在OnClickListener中对动画进行空指针检查
示例代码:
BitmapAnimation animation = (BitmapAnimation) findViewById(R.id.animation);
animation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (animation != null && !animation.isRunning()) {
animation.start();
}
}
});
在OnClickListener中首先检查动画是否为空,如果不为空且动画未在运行,则启动动画。这样可以避免由于动画为空或已经在运行而导致的NullPointerException异常。