迭代光标时的nullReference

时间:2015-07-11 14:44:34

标签: java android

我一直在尝试发送自定义对象的ArrayList。所以我让我的Class Parcelable。这是我的班级:

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中,所以我尝试使用以下代码将Cursor转换为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);
    }

}
}

当我运行应用程序时,我可以在适配器的帮助下填充列表视图。这意味着我有了光标。现在单击ListItem后,我在此行中获得nullReference异常:

sendSongsArrayToPlayNow.add(new SongInformation(cu));

调试时我可以看到Cursor的计数为87,即它已被初始化。我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

更改

ArrayList<SongInformation> sendSongsArrayToPlayNow;

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

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