【问题标题】:Android can`t get images from api with picassoAndroid无法使用毕加索从api获取图像
【发布时间】:2016-11-14 05:52:32
【问题描述】:

我正在尝试获取api的图片并将它们放入列表中,但是当我写毕加索时,Picasso.with field (context) context 强调了红线,我不应该在哪里?请告诉我。

适配器:

 class MyAdapter extends ArrayAdapter<Places> {


    public MyAdapter(Context context, List<Places> objects) {
        super(context, R.layout.list_item, objects);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.nameOfPlace = (TextView) rowView.findViewById(R.id.name_Id);
            holder.subcategory_name = (TextView) rowView.findViewById(R.id.subcategory_Id);
            holder.geometryName = (TextView) rowView.findViewById(R.id.geometry_Id);
            holder.imageView = (ImageView) rowView.findViewById(R.id.imageView);
            holder.imageView2 = (ImageView) rowView.findViewById(R.id.imageView2);
            holder.rating = (TextView) rowView.findViewById(R.id.rating_Id);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        Places places = getItem(position);
        holder.nameOfPlace.setText(places.getName());
        holder.subcategory_name.setText(places.getSubcategory_name());
        holder.geometryName.setText(places.getGeometry_name());

        holder.imageView2.setImageResource(R.drawable.love_5033);
        holder.rating.setText("Рейтинг: " + places.getRating() + "              В избранном: " + places.getFavorite());

 //There is Picasso
        Picasso.with(context).load(places.getCsv_image()) .into(holder.imageView);



        return rowView;
    }

    class ViewHolder {

        public TextView nameOfPlace;
        public TextView subcategory_name;
        public TextView geometryName;
        public TextView rating;
        public ImageView imageView;
        public ImageView imageView2;
    }
}

地点类:

    public class Places implements Serializable {


    String name;
    String geometry_name;
    String rating;
    String subcategory_name;
    String favorite;
    String csv_image;

    public Places(String name, String geometry_name, String rating,String subcategory_name, String favorite,String csv_image) {
        this.name = name;
        this.geometry_name = geometry_name;
        this.rating = rating;
        this.subcategory_name = subcategory_name;
        this.favorite = favorite;
        this.csv_image = csv_image;

    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getGeometry_name() {
        return geometry_name;
    }

    public void setGeometry_name(String geometry_name) {
        this.geometry_name = geometry_name;
    }

    public String getRating() {
        return rating;
    }

    public void setRating(String rating) {
        this.rating = rating;
    }

    public String getSubcategory_name() {
        return subcategory_name;
    }

    public void setSubcategory_name(String subcategory_name) {
        this.subcategory_name = subcategory_name;
    }

    public String getFavorite() {
        return favorite;
    }

    public void setFavorite(String favorite) {
        this.favorite = favorite;
    }

    public String getCsv_image() {
        return csv_image;
    }

    public void setCsv_image(String csv_image) {
        this.csv_image = csv_image;
    }
}

初始化适配器,改造成功:

  Retrofit.getPlaces(new Callback<List<Places>>() {
        @Override
        public void success(final List<Places> places, Response response) {


            FileOutputStream fos = null;
            try {
                fos = openFileOutput("countries_file", Context.MODE_PRIVATE);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(places);
                oos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            listView.setAdapter(new MyAdapter(MainActivity.this, places));
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent mIntent = new Intent(MainActivity.this, MapActivity.class);
                    Places plasez = places.get(position);
                    mIntent.putExtra("key", plasez);
                    startActivity(mIntent);
                }
            });


        }

        @Override
        public void failure(RetrofitError error) {
            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();



        }
    });




}

完整的 MainActivity:

public class MainActivity extends AppCompatActivity {

final String LOG_TAG = "myLogs";

ListView listView;

TextView name;
TextView subcategory_name;
TextView geometry_name;
TextView rating;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    name = (TextView) findViewById(R.id.name_Id);
    subcategory_name = (TextView) findViewById(R.id.subcategory_Id);
    geometry_name = (TextView) findViewById(R.id.geometry_Id);
    rating = (TextView) findViewById(R.id.rating_Id);
    listView = (ListView)findViewById(R.id.listVieww);




    FileInputStream fis;
    try {
        fis = openFileInput("countries_file");
        ObjectInputStream ois = new ObjectInputStream(fis);
        final ArrayList<Places> returnlist = (ArrayList<Places>) ois.readObject();
        ois.close();
        listView.setAdapter(new MyAdapter(this, returnlist));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent mIntent = new Intent(MainActivity.this, MapActivity.class);
                Places placez = returnlist.get(position);
                mIntent.putExtra("key", placez);
                startActivity(mIntent);

            }
        });
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }





    Retrofit.getPlaces(new Callback<List<Places>>() {
        @Override
        public void success(final List<Places> places, Response response) {
            Log.d(LOG_TAG, "получили данные");

            FileOutputStream fos = null;
            try {
                fos = openFileOutput("countries_file", Context.MODE_PRIVATE);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(places);
                oos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            listView.setAdapter(new MyAdapter(MainActivity.this, places));
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent mIntent = new Intent(MainActivity.this, MapActivity.class);
                    Places plasez = places.get(position);
                    mIntent.putExtra("key", plasez);
                    startActivity(mIntent);
                }
            });


        }

        @Override
        public void failure(RetrofitError error) {
            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();



        }
    });




}




class MyAdapter extends ArrayAdapter<Places> {
    private Context context;




    public MyAdapter(Context context, List<Places> objects) {
        super(context, R.layout.list_item, objects);


    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.nameOfPlace = (TextView) rowView.findViewById(R.id.name_Id);
            holder.subcategory_name = (TextView) rowView.findViewById(R.id.subcategory_Id);
            holder.geometryName = (TextView) rowView.findViewById(R.id.geometry_Id);
            holder.imageView = (ImageView) rowView.findViewById(R.id.imageView);
            holder.imageView2 = (ImageView) rowView.findViewById(R.id.imageView2);
            holder.rating = (TextView) rowView.findViewById(R.id.rating_Id);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        Places places = getItem(position);
        holder.nameOfPlace.setText(places.getName());
        holder.subcategory_name.setText(places.getSubcategory_name());
        holder.geometryName.setText(places.getGeometry_name());

        holder.imageView2.setImageResource(R.drawable.love_5033);
        holder.rating.setText("Рейтинг: " + places.getRating() + "              В избранном: " + places.getFavorite());
        Picasso.with(context).load(places.getCsv_image()) .into(holder.imageView);


        return rowView;
    }

    class ViewHolder {

        public TextView nameOfPlace;
        public TextView subcategory_name;
        public TextView geometryName;
        public TextView rating;
        public ImageView imageView;
        public ImageView imageView2;

    }
 }

}

【问题讨论】:

  • 您遇到的错误是什么?你在哪里初始化了“上下文”字段?
  • 如果您使用活动,请将活动上下文传递给适配器,如果您使用片段,则将 getActivity().getApplicationContext() 传递给适配器,然后在毕加索的适配器中使用该上下文
  • Picasso.with field (context) context 强调红线,并且我有一个 exeption context is must not be null
  • 我在用activity,怎么办?

标签: android listview retrofit picasso


【解决方案1】:
private Context context;

public MyAdapter(Context context, List<Places> objects) {
super(context, R.layout.list_item, objects);
this.context = context;}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();
        holder.nameOfPlace = (TextView) rowView.findViewById(R.id.name_Id);
        holder.subcategory_name = (TextView) rowView.findViewById(R.id.subcategory_Id);
        holder.geometryName = (TextView) rowView.findViewById(R.id.geometry_Id);
        holder.imageView = (ImageView) rowView.findViewById(R.id.imageView);
        holder.imageView2 = (ImageView) rowView.findViewById(R.id.imageView2);
        holder.rating = (TextView) rowView.findViewById(R.id.rating_Id);
        rowView.setTag(holder);
    } else {
        holder = (ViewHolder) rowView.getTag();
    }

    Places places = getItem(position);
    if (!TextUtils.isEmpty(places.getCsv_image())) {
            Picasso.with(context).into(holder.imageView)
                    .load(places.getCsv_image());}

    return rowView;
}

【讨论】:

  • compile 'com.squareup.picasso:picasso:2.5.0' add in gradle app file
  • 我添加了,在您的示例中找不到
  • Picasso.with(context) .load(places.getCsv_image()) .into(holder.imageView);
  • 出现异常 java.lang.IllegalArgumentException: Context 不能为空。
  • 你遵守所有代码了吗?我上面已经写过了。我认为你没有初始化 this.context = context;
【解决方案2】:

试试这个:

Private Context context;

public MyAdapter(Context context, List<Places> objects) {
   super(context, R.layout.list_item, objects);
   this.context = context;
}

...

【讨论】:

  • 出现异常 java.lang.IllegalArgumentException: Context 不能为空。
  • @Azarnoy 尽可能显示您的 MainAcitvity 的完整代码。
  • @Azarnoy 你有没有改变你的适配器类..?那就是您发布的内容或其部分实施的内容。?因为我看到你错过了构造函数中的一行。 'this.context = context' 在那个适配器中。?
  • 我没有改变任何东西,它复制了我的活动
  • @Azarnoy 我说的是你的 MyAdapter。请告诉我,您是否使用此处建议的答案更改了您的 MyAdapter 类。?
【解决方案3】:

如下写:

class MyAdapter extends ArrayAdapter<Places> {

Context context=null;


public MyAdapter(Context context, List<Places> objects) {
    super(context, R.layout.list_item, objects);
    this.context=context;
}

【讨论】:

  • 有一个异常:java.lang.IllegalArgumentException:上下文不能为空。
  • 这是因为您从初始化适配器的位置发送 null。显示您正在初始化和设置适配器的代码。
【解决方案4】:

试试这个:

Context context = holder.imageView.getContext();
    String st = android.get(position).getImage();
    String image = st.replace("\\", "");
    if (image.equals("")) {

        Picasso.with(context).load(R.drawable.profilepi).into(holder.imageView);
    } else {

        Picasso.with(context).load("http://ec2-54-187-18-119.us-west-2.compute.amazonaws.com/lived2d/" + image).resize(100, 100).centerCrop().into(holder.imageView);
    }

}

在开头初始化上下文

【讨论】:

  • 我有异常 java.lang.IllegalArgumentException: Path must not be empty.
  • 您在哪一行收到此错误?上下文一还是毕加索一?
猜你喜欢
  • 2018-01-01
  • 1970-01-01
  • 2021-10-01
  • 2016-09-03
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
相关资源
最近更新 更多