0

i want to use of animation drawable in my list but when i start the listActivity it crashs i have learned from this , when i remove this 2 animation codes

 AnimationDrawable frameAnimation = (AnimationDrawable) GamePic.getBackground();
        frameAnimation.start();

,it works animated by itself in my phone (p7 with android 4.4 ) but in other phones i have tried (asus memopad 7 , sony c ) it works with no animated drawables ! attent when i use of this up codes all phones just crashes below is my compelet code , what is wrong ?

public class MyList extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.games_list);

    setListAdapter(new AA());
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    Toast.makeText(MyList.this, "item" + position + "is clicked", Toast.LENGTH_SHORT).show();

}


class AA extends ArrayAdapter<String> {

    public AA() {
        super(MyList.this, R.layout.games_list_layout, Resources2.GameListArray);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.games_list_layout, parent, false);
        }
        TextView GameName = (TextView) row.findViewById(R.id.textView1);
        ImageView GamePic = (ImageView) row.findViewById(R.id.imageView2);
        ImageView Cartridge = (ImageView) row.findViewById(R.id.imageView1);

        GameName.setText(Resources2.GameListArray[position]);
        GamePic.setImageResource(Resources2.GamePicArray[position]);
        Cartridge.setImageResource(Resources2.CartridgeFrame[Resources2.cartridgeColor[position]]);

        AnimationDrawable frameAnimation = (AnimationDrawable) GamePic.getBackground();
        frameAnimation.start();

        return row;

    }
}

}

thanks

roz
  • 27
  • 1
  • 8

2 Answers2

0

As the documentation describes, you should wait until the View is attached to the window before starting animation. Therefor, you should add an OnAttachStateChangeListener to the view that will execute when it has been attached, and start the animation from there.

ImageView img = (ImageView)v.findViewById(R.id.image);
img.setBackgroundResource(R.drawable.anim);
img.addOnAttachStateChangeListener(new  View.OnAttachStateChangeListener() {
  @Override
  public void onViewAttachedToWindow(View v) {
  AnimationDrawable animation = (AnimationDrawable) v.getBackground();
  animation .start();
  }

@Override
public void onViewDetachedFromWindow(View v) {
}
});

OR Use Movie Class if You are using GIF. here are some Learning Links, this , this and this also this quistion

Community
  • 1
  • 1
Sajidkhan
  • 608
  • 7
  • 16
0

i have found my problem !

i had used GamePic.setImageResource(Resources2.GamePicArray[position]);

but i should use

GamePic.setBackgroundResource(Resources2.GamePicArray[position]);

now it works fine !

roz
  • 27
  • 1
  • 8