There is a RecyclerView list that has two items: TextView and ImageView . ImageView has a default image, TextView contains url for different pictures on the Internet, Textview filled with data from a AsyncTask using AsyncTask . It is necessary that after creating the RecyclerView list instead of the default picture, the picture is loaded by a specific link. After creating the adapter, I try to fill the ImageView with data using Picasso, but I get an error: Illegalstateexception target host is null .

There is an option to upload a picture in ViewHolder , in the onBindViewHolder method, but then if you have a bad internet connection, the application download takes too long, which does not work. It is necessary that it is filled with a standard picture, and then loaded.

ListItem.java

 public class ListItem { private String head; private String desc; private Drawable webSiteImage; private List<String> headList; private List<String> urlList; private List<String> imgUrlList; private String imgUrl; public ListItem() { } public String getHead() { return head; } public String getDesc() { return desc; } public void setHead(String head) { this.head = head; } public void setDesc(String desc) { this.desc = desc; } public List<String> getHeadList() { return headList; } public List<String> getUrlList() { return urlList; } public void setHeadList(List<String> headList) { this.headList = headList; } public void setUrlList(List<String> urlList) { this.urlList = urlList; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; } public List<String> getImgUrlList() { return imgUrlList; } public void setImgUrlList(List<String> imgUrlList) { this.imgUrlList = imgUrlList; } public Drawable getWebSiteImage() { return webSiteImage; } public void setWebSiteImage(Drawable webSiteImage) { this.webSiteImage = webSiteImage; } } 

MyRecViewAdapter.java adapter

 public class MyRecViewAdapter extends RecyclerView.Adapter<MyRecViewAdapter.ViewHolder>{ private List<ListItem> listItems; private Context context; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView tvHead; public TextView tvDesc; public ImageView ivWebImage; public ViewHolder(View itemView) { super(itemView); tvHead = (TextView) itemView.findViewById(R.id.tv_head); tvDesc = (TextView) itemView.findViewById(R.id.tv_desc); ivWebImage = (ImageView) itemView.findViewById(R.id.ivWebImage); } } public MyRecViewAdapter(List<ListItem> listItems, Context context) { this.listItems = listItems; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_item, parent, false); //return our view return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { ListItem listItem = listItems.get(position); holder.tvHead.setText(listItem.getHead()); holder.tvDesc.setText(listItem.getDesc()); holder.ivWebImage.setImageDrawable(listItem.getWebSiteImage()); /*Picasso.with(context).load(listItems.get(position).getImgUrl()) .error(R.drawable.ic_launcher_foreground) .placeholder(R.drawable.ic_launcher_foreground) .into(holder.ivWebImage);*/ } @Override public int getItemCount() { return listItems.size(); } } 

MainActivity.java

 public class MainActivity extends AppCompatActivity { public static final String TAG = "mLog"; @BindView(R.id.recycler_view) RecyclerView recyclerView; @BindView(R.id.button) Button button; private RecyclerView.Adapter adapter; private RecyclerView.LayoutManager mLayoutManager; private ListItem item; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); OperationsDB operationsDB = new OperationsDB(this); operationsDB.execute(); mLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(mLayoutManager); try { item = operationsDB.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } Drawable d = getResources().getDrawable(R.drawable.ic_launcher_background); List<ListItem> itemRecView = new ArrayList<>(); for (int i = 0; i < item.getHeadList().size(); i++) { ListItem listItem = new ListItem(); listItem.setHead(item.getHeadList().get(i)); listItem.setDesc(item.getUrlList().get(i)); listItem.setImgUrl(item.getImgUrlList().get(i)); listItem.setWebSiteImage(d); itemRecView.add(listItem); } adapter = new MyRecViewAdapter(itemRecView, this); recyclerView.setAdapter(adapter); MyRecViewAdapter.ViewHolder viewHolder = new MyRecViewAdapter.ViewHolder( recyclerView); Picasso.with(this).load(listItem.getImgUrlList.get(1)) .into(viewHolder.ivWebImage); } } private static class OperationsDB extends AsyncTask<Void, Void, ListItem> { Document document; private final WeakReference<MainActivity> activityWeakReference; OperationsDB(MainActivity context) { activityWeakReference = new WeakReference<>(context); } @Override protected ListItem doInBackground(Void... voids) { /* Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° класа для заполнСния ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΉ Π΄Π°Π½Π½Ρ‹ΠΌΠΈ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌΠΈ Π±ΡƒΠ΄ΡƒΡ‚ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½Ρ‹ ΠΈΡ‚Π΅ΠΌΡ‹ RecyclerView */ ListItem listItem = new ListItem(); // ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ Π²Π΅Π± ссылок, ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹Ρ… ΠΈΠ· Π‘Π” List<String> urls = new ArrayList<>(); urls.addAll(getData().getUrlList()); // Π—Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ ссылок ΠΈ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΎΠ² сайтов listItem.setUrlList(urls); listItem.setHeadList(getData().getHeadList()); List<String> list = new ArrayList<>(); for (int i = 0; i < urls.size(); i++) { try { document = Jsoup.connect(urls.get(i)).get(); } catch (IOException e) { e.printStackTrace(); } Element img = document.select("img").first(); String imgSrc = img.absUrl("src"); list.add(imgSrc); } listItem.setImgUrlList(list); return listItem; } private ListItem getData() { DBAdapter db; db = Room.databaseBuilder(activityWeakReference.get(), DBAdapter.class, DB_NAME).build(); WebSitesDao webSitesDao = db.webSitesDao(); ListItem listItem = new ListItem(); listItem.setHeadList(webSitesDao.getColumn()); listItem.setUrlList(webSitesDao.getUrl()); return listItem; } } } 
  • I'm certainly not sure, but most likely the error here is not in Picasso, but in the tin that occurs in the loop where the itemRecView sheet is filled. - Android Android
  • No, an error occurs when using Picasso. Without this line of code, everything works. - Sergey
  • What comes right after setAdapter? you create some kind of viewholder for some purpose ... And you didn’t attach or read the glass-space, on those lines everything falls and goes. - Yura Ivanov
  • I create a viewholder to refer to the recyclerview and change its contents with a picture obtained using picasso - Sergey
  • @ Sergey, too, am inclined to think that you have brakes because of the list. First, you do AsyncTask, immediately take a piece of data obtained from another thread ... it seems to me - against the background of common brakes, receiving data in the adapter is listItems.get (position) .getImgUrl () leads to normal such performance problems at startup . - Andrey Mihalev

0