ImageView上的setBackgroundResource返回null

时间:2014-08-31 11:27:45

标签: java android listview imageview jquery-animate

我在我的android项目中使用ListFragment。我决定制作一个listView,每个listview项目都包含音乐频率imageView。我将尝试为音乐频率的imageView设置动画。每个listView都有一个名为play的按钮,一旦点击了该播放,音乐频率就会开始动画。

public View onCreateView(LayoutInflater inflater,
         ViewGroup container,  Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragmentmusiclist, container, false);
    lv = (ListView) v.findViewById(R.id.list);


    mp = new MediaPlayer();
    mp = null;

    Song s1 = new Song("Love Story",(float) 2.5, onMusicPlayListener);
    Song s2 = new Song("Sad Story",(float) 1.0, onMusicPlayListener);
    Song s3 = new Song("Breakup Story",(float) 3.5, onMusicPlayListener);

    songs = new ArrayList<Song>();
    songs.add(s1);
    songs.add(s2);
    songs.add(s3);

    adapter = new MusicListAdapter(getActivity(),songs);
    setListAdapter(adapter);


    return super.onCreateView(inflater, container, savedInstanceState);
}

OnClickListener onMusicPlayListener = new OnClickListener() {

@Override
public void onClick(View v) {
  final int position = getListView().getPositionForView((LinearLayout)v.getParent());
  animMusicFreq = (ImageView) v.findViewById(R.id.music_anim);
  animMusicFreq.setBackgroundResource(R.anim.anim_music);
  animMusicFreq.post(new Runnable() {

    @Override
    public void run() {
        AnimationDrawable frameAnimation =
            (AnimationDrawable) animMusicFreq.getBackground();
                    frameAnimation.start();
    }

});

1 个答案:

答案 0 :(得分:1)

您正在onCreateView内对视图进行夸大,但是您没有将其传递给系统。相反,你告诉它自己膨胀(你返回super)。它没有任何意义。

您应该像这样返回v视图:

public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,  Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragmentmusiclist, container, false);
    // ...
    return v;
}