nullReference虽然迭代游标

问题描述:

我一直在尝试发送ArrayList的自定义对象。所以我制作了我的课堂讲义。这是我的班级:nullReference虽然迭代游标

package com.abhi8569.musicplayer; 

import android.content.ContentResolver; 
import android.content.ContentUris; 
import android.content.Context; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Parcel; 
import android.os.Parcelable; 

import java.io.InputStream; 
public class SongInformation implements Parcelable { 

private int albumID; 
private String queryTitle; 
private String queryAlbum; 
private String queryArtist; 
private int queryDuration; 
private String filepath; 
private String _id; 


public int getAlbumID() { 
    return albumID; 
} 

public void setAlbumID(int albumID) { 
    this.albumID = albumID; 
} 

public SongInformation(Cursor cursor) { 
    albumID=cursor.getInt(0); 
    queryTitle = cursor.getString(1); 
    queryArtist = cursor.getString(2); 

    queryAlbum = cursor.getString(3); 
    queryDuration = cursor.getInt(4); 
    filepath = cursor.getString(5); 
    _id = cursor.getString(6); 

} 
public String getQueryAlbum() { 
    return queryAlbum; 
} 

public void setQueryAlbum(String queryAlbum) { 
    this.queryAlbum = queryAlbum; 
} 

public String getQueryArtist() { 
    return queryArtist; 
} 

public void setQueryArtist(String queryArtist) { 
    this.queryArtist = queryArtist; 
} 

public int getQueryDuration() { 
    return queryDuration; 
} 

public void setQueryDuration(int queryDuration) { 
    this.queryDuration = queryDuration; 
} 

public String getFilepath() { 
    return filepath; 
} 

public void setFilepath(String filepath) { 
    this.filepath = filepath; 
} 

public String get_id() { 
    return _id; 
} 

public void set_id(String _id) { 
    this._id = _id; 
} 

public String getQueryTitle() { 
    return queryTitle; 
} 

public void setQueryTtile(String queryTitle) { 
    this.queryTitle = queryTitle; 
} 

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeInt(this.albumID); 
    dest.writeString(this.queryTitle); 
    dest.writeString(this.queryAlbum); 
    dest.writeString(this.queryArtist); 
    dest.writeInt(this.queryDuration); 
    dest.writeString(this.filepath); 
    dest.writeString(this._id); 
} 

protected SongInformation(Parcel in) { 
    this.albumID = in.readInt(); 
    this.queryTitle = in.readString(); 
    this.queryAlbum = in.readString(); 
    this.queryArtist = in.readString(); 
    this.queryDuration = in.readInt(); 
    this.filepath = in.readString(); 
    this._id = in.readString(); 
} 

public static final Parcelable.Creator<SongInformation> CREATOR = new Parcelable.Creator<SongInformation>() { 
    public SongInformation createFromParcel(Parcel source) { 
     return new SongInformation(source); 
    } 

    public SongInformation[] newArray(int size) { 
     return new SongInformation[size]; 
    } 
}; 
} 

到目前为止,我已将所有音乐信息存储在光标中。我想将它保存在一个ArrayList,所以我想转换光标使用此代码ArrayList的:

public class SongsTab extends Fragment implements AdapterView.OnItemClickListener { 

ListView lv; 
Cursor cu; 
View v; 
ArrayList<SongInformation> sendSongsArrayToPlayNow; 
SongListViewAdapter adapt; 

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    v = inflater.inflate(R.layout.activity_songs_tab, container, false); 

    lv = (ListView) v.findViewById(R.id.songs_tab_listView); 
    new MyTask().execute(); 
    lv.setOnItemClickListener(this); 

    return v; 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    int pos = position; 
    cu.moveToFirst(); 
    while (cu.moveToNext()) { 
     sendSongsArrayToPlayNow.add(new SongInformation(cu)); 
    } 
    Intent i = new Intent(getActivity(), PlayingNow.class); 
    i.putExtra("type", "songs"); 
    i.putParcelableArrayListExtra("data", sendSongsArrayToPlayNow); 
    i.putExtra("position", pos); 
    startActivity(i); 
} 

class MyTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 

    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     cu = MainActivity.getMusicCursorFromMainActivity(); 
     adapt = new SongListViewAdapter(v.getContext(), cu); 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     lv.setAdapter(adapt); 
    } 

} 
} 

当我运行应用程序,我能来填充列表视图与适配器的帮助。这意味着我有光标。现在,在点击列表项我得到nullReference异常在这一行:

sendSongsArrayToPlayNow.add(new SongInformation(cu)); 

在调试,我可以看到光标有87计,即它已被初始化。我在哪里做错了?

+1

问题不在于游标,而在于'sendSongsArrayToPlayNow'。它没有被初始化。 – Codebender

变化

ArrayList<SongInformation> sendSongsArrayToPlayNow; 

ArrayList<SongInformation> sendSongsArrayToPlayNow = new ArrayList<SongInformation>(); 

您需要将您的引用变量链接到new ArrayList<>()

+0

哎呀...我错过了。谢谢! – abhi8569