如何更改屏幕外的ListView项中的数据?

时间:2017-10-02 16:38:48

标签: java android listview android-adapter

我正在使用工具栏和ListView处理我的音乐播放器应用程序。在我的工具栏中,我有一些按钮,允许从ListView播放下一个和上一个音频,并在每个ListView项目中更改ImageButton。但是,例如,如果我当前播放的音频位于ListView的底部,而下一个音频根本不可见,那么当我按下按钮" next"我的应用程序崩溃,因为我无法在ListView项目中更改ImageButton,这是在屏幕之外。 This is how it looks like.这是我的适配器代码:

public class MyAdapter extends BaseAdapter
{
    Context context;
    MusicList music_list;
    LayoutInflater layout_inflater;

    MyAdapter(Context context, MusicList music_list)
    {
        this.context = context;
        this.music_list = music_list;
        this.layout_inflater = (LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount()
    {
        return music_list.Size();
    }

    @Override
    public Object getItem(int position)
    {
        return music_list.GetAudio(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
        {
            view = layout_inflater.inflate(R.layout.item, parent, false);
        }

        view.setTag(position);
        AudioFile audio_file = GetAudioFile(position);

        ((TextView) view.findViewById(R.id.title)).setText(audio_file.GetTitle());
        ((TextView) view.findViewById(R.id.artist)).setText(audio_file.GetArtist());
        ((TextView) view.findViewById(R.id.time)).setText(audio_file.GetDurationString());

        ImageView play_pause = (ImageView) view.findViewById(R.id.play_pause);
        play_pause.setImageResource(android.R.drawable.ic_media_play);

        return view;
    }

    AudioFile GetAudioFile(int position)
    {
        return ((AudioFile) getItem(position));
    }
}

我的崩溃日志:

10-02 16:53:05.577 12344-12344/agolubeff.mymusicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: agolubeff.mymusicplayer, PID: 12344
java.lang.RuntimeException: Error receiving broadcast Intent { act=agolubeff.mymusicplayer flg=0x10 (has extras) } in agolubeff.mymusicplayer.SongList$2@398ef693
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:876)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    at agolubeff.mymusicplayer.SongList.UpdateUI(SongList.java:141)
    at agolubeff.mymusicplayer.SongList.access$300(SongList.java:30)
    at agolubeff.mymusicplayer.SongList$2.onReceive(SongList.java:99)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

有人知道如何处理它吗?

0 个答案:

没有答案