繁体   English   中英

android studio picasso java.lang.IllegalArgumentException:目标不得为null

[英]android studio picasso java.lang.IllegalArgumentException: Target must not be null

我正在尝试通过毕加索加载图像。 一切正常,除了毕加索试图加载从URL获得的图像的部分。 它被错误消息卡住:

java.lang.IllegalArgumentException:目标不得为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的新手。 谢谢

我没有在列表视图中加载图像,而是在另一活动中加载了另一图像。 完全没有想到那部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM