android studio picasso java.lang.IllegalArgumentException:Target不能为null

时间:2016-03-30 12:44:46

标签: java android-studio android-volley picasso illegalargumentexception

我正试图通过毕加索加载图像。除了Picasso尝试加载我从网址获取的图像的部分外,一切正常。它停留在错误消息中:

  

java.lang.IllegalArgumentException:Target不能为null。在   Picasso.with(mContext)

但我认为真正的问题是

  

.into(holder.coverArtwork);

我已经尝试过这么多,但我只是看不出有什么问题。当我尝试运行它时,它看起来很好但是崩溃了。 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 studio很新。谢谢

1 个答案:

答案 0 :(得分:0)

我没有在listview中加载图像,而是在另一个活动中加载另一个图像。根本没有想到这一点。