دنبال کننده ها

۱۳۹۶ شهریور ۹, پنجشنبه

java - android - 'android.os.NetworkOnMainThreadException' when using RecyclerView.Adapter

[ad_1]



I am trying to set an Image URL on ImageView inside a class which extends RecyclerView.Adapter<>.



I am setting the image URL inside method onBindViewHolder(SingleItemRowHolder holder, int i).



Below is my Code:



 @Override
public void onBindViewHolder(SingleItemRowHolder holder, int i)

SingleItemModel singleItem = itemsList.get(i);

holder.tvTitle.setText(singleItem.getName());
//holder.itemImage.setImageURI(Uri.parse(singleItem.getUrl()));
try
URL url = new URL(singleItem.getUrl());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
holder.itemImage.setImageBitmap(bmp);
catch (Exception e)
e.printStackTrace();





I can set the text but while setting the image URL on ImageView, I am getting this error in catch.
The error is android.os.NetworkOnMainThreadException.
Also, the URL is hard-coded inside the class. Which means I am not using the Internet.



Any help would be appreciated.



Thanks in advance.




[ad_2]

لینک منبع