我尝试使用AsyncTask
加载专辑封面,但我得到NullPointerException
:
ImageLoader的:
public class ImageLoader extends AsyncTask<String, Void, Bitmap>{
Context mcontext;
private ImageView image;
Cursor cursor;
private Bitmap artwork = null;
SongsListAdapter songsAdapter;
public ImageLoader(ImageView image) {
this.image = image;
}
protected Bitmap doInBackground(String... uri) {
for (int i=0; i < songsAdapter.getCount(); i++) {
cursor.moveToPosition(i);
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Long albumid = cursor.getLong(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Uri art = ContentUris.withAppendedId(sArtworkUri, albumid);
ContentResolver res = mcontext.getContentResolver();
InputStream in = null;
try {
in = res.openInputStream(art);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
artwork = BitmapFactory.decodeStream(in);
}
return null;
}
protected void onPostExecute(Void arg) {
// Display the image
image.setImageBitmap(artwork);
}
}
SongsListAdapter :
public class SongsListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<MusikUntil> songList;
public SongsListAdapter(Context context, ArrayList<MusikUntil> list) {
this.mContext = context;
this.songList = list;
}
public int getCount() {
return songList.size();
}
public MusikUntil getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.song_item, null);
}
ImageView mImgIcon = (ImageView)convertView.findViewById(R.id.list_image);
TextView mTxtJudul = (TextView)convertView.findViewById(R.id.Judul_Musik);
TextView mTxtMusisi = (TextView)convertView.findViewById(R.id.Artis);
new ImageLoader(mImgIcon).execute();
mTxtJudul.setText(songList.get(position).getSongName());
mTxtMusisi.setText(songList.get(position).getArtistName());
return convertView;
}
public void setSongsList (ArrayList<MusikUntil> list) {
songList = list;
this.notifyDataSetChanged();
}
}
logcat的:
Process: com.karyaanaknegeri.musikq, PID: 5736
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.karyaanaknegeri.musikq.adapters.SongsListAdapter.getCount()' on a null object reference
at com.karyaanaknegeri.musikq.loader.ImageLoader.doInBackground(ImageLoader.java:40)
at com.karyaanaknegeri.musikq.loader.ImageLoader.doInBackground(ImageLoader.java:28)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:2)
<强> 1。 ImageLoader中的问题
您只声明了SongsListAdapter songsAdapter;
。在使用之前,您必须首先初始化此对象。
您没有初始化Cursor cursor;
,因此它也会抛出NullPointerException
。
<强> 2。 SongsListAdapter中的问题
变化:
public MusikUntil getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
到
public MusikUntil getItem(int position) {
return songList.get(position);
}
public long getItemId(int position) {
return position;
}
第3。提示
您的代码看起来像某些教程中的复制粘贴。如果您遇到AsyncTask
问题,请尝试Picasso或Universal Image Loader for Android。这些库非常易于使用,可以帮助您处理加载图像。