2015-07-11 193 views
0

我一直在嘗試發送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

回答

0

變化

ArrayList<SongInformation> sendSongsArrayToPlayNow; 

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

您需要將您的引用變量鏈接到new ArrayList<>()

+0

哎呀...我錯過了。謝謝! – abhi8569