我正试图通过Picasso加载图像。除了毕加索试图加载我从url获得的图像的部分外,一切都在工作。它被错误消息卡住了:
java.lang.IollegalArgumentException:目标不能为null。在Picasso.with(mContext)
然而,我认为真正的问题是
.插入(支架.罩艺术作品);。
我试了这么多,但就是不知道出了什么问题。它看起来很好,但当我试图运行它时崩溃了。log.d向我显示url是正确的,上下文有一个值。这两个领域都是我研究过的。
下面是我的代码。
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONObject;
public class ParseJSON extends BaseAdapter {
Context mContext;
LayoutInflater mInflater;
JSONArray mJsonArray;
public ParseJSON(Context context, LayoutInflater inflater) {
mContext = context;
mInflater = inflater;
mJsonArray = new JSONArray();
Log.v("Context Value:", "" + mContext);
}
public void updateData(JSONArray jsonArray) {
// update the adapter's dataset
mJsonArray = jsonArray;
notifyDataSetChanged();
}
@Override public int getCount() {return mJsonArray.length();
}
@Override public JSONObject getItem(int position) {
// your particular dataset uses String IDs
// but you have to put something in this method
return mJsonArray.optJSONObject(position);
}
@Override public long getItemId(int position) {
// your particular dataset uses String IDs
// but you have to put something in this method
return position;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// check if the view already exists
// if so, no need to inflate and findViewById again!
if (convertView == null) {
// Inflate the custom row layout from your XML.
convertView = mInflater.inflate(R.layout.music_list_layout, null);
// create a new "Holder" with subviews
holder = new ViewHolder();
holder.musicSong = (TextView) convertView.findViewById(R.id.musicSong);
holder.musicArtist = (TextView) convertView.findViewById(R.id.musicArtist);
holder.musicGerne = (TextView) convertView.findViewById(R.id.musicGenre);
holder.musicTime = (TextView) convertView.findViewById(R.id.musicTime);
holder.coverArtwork = (ImageView) convertView.findViewById(R.id.songcover);
// hang onto this holder for future recyclage
convertView.setTag(holder);
} else {
// skip all the expensive inflation/findViewById
// and just get the holder you already made
holder = (ViewHolder) convertView.getTag();
}
// Get the current book's data in JSON form
JSONObject jsonObject = getItem(position);
// See if there is a cover ID in the Object
if (jsonObject.has("artwork")) {
// If so, grab the Cover ID out from the object
String artworkUrl = jsonObject.optString("artwork");
Log.d("Image Url", artworkUrl);
// Construct the image URL (specific to API)
String imageURL = artworkUrl;
// Use Picasso to load the image
// Temporarily have a placeholder in case it's slow to load
Picasso.with(mContext)
.load(imageURL)
.placeholder(R.drawable.sportmuziek)
.into(holder.coverArtwork);
} else {
// If there is no cover ID in the object, use a placeholder
holder.coverArtwork.setImageResource(R.drawable.sportmuziek);
}
// Grab the title and author from the JSON
String songname = "";
String artistname = "";
String musicgerne = "";
String musictime = "";
if (jsonObject.has("track_name")) {
songname = jsonObject.optString("track_name");
}
if (jsonObject.has("artist")) {
artistname = jsonObject.optString("artist");
}
if (jsonObject.has("tags")) {
musicgerne = jsonObject.optString("tags");
}
if (jsonObject.has("time")) {
musictime = jsonObject.optString("time");
}
// Send these Strings to the TextViews for display
holder.musicSong.setText(songname);
holder.musicArtist.setText(artistname);
holder.musicGerne.setText(musicgerne);
holder.musicTime.setText(musictime);
return convertView;
}
// this is used so you only ever have to do
// inflation and finding by ID once ever per View
private static class ViewHolder {
public TextView musicSong;
public TextView musicArtist;
public TextView musicGerne;
public TextView musicTime;
public ImageView coverArtwork;
}
}
我真的需要一些帮助,因为我是android工作室的新手。感谢
i没有在listview中加载图像,而是在另一个活动中加载另一个图像。那部分根本没想清楚。