search

Home  >  Q&A  >  body text

android - 解析API但是图片是String类型 配合RecyclerView

想配合RecyclerView使用
但是在onBindViewHolder方法中

 @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Utility.getImageLoader().displayImage(newses.get(position).getImages().get(0),
                holder.news_image,Utility.getDisplayImageOptions());
        holder.news_title.setText(newses.get(position).getTitle());

    }
   

会出现此问题

                java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
                                                                                     at com.example.axtonsun.zhihudaily.Adapter.RvAdapter.onBindViewHolder(RvAdapter.java:39)
                                                                                     at com.example.axtonsun.zhihudaily.Adapter.RvAdapter.onBindViewHolder(RvAdapter.java:20)
                                                                                     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
                                                                                     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
                                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
                                                                                     
                                                                                        

在Utility.getImageLoader()中

 public static ImageLoader getImageLoader() {
        return ImageLoader.getInstance();
    }

    public static DisplayImageOptions getDisplayImageOptions() {
        return new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.start0) //设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.drawable.start1)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.start2)  //设置图片加载/解码过程中错误时候显示的图片
                .displayer(new FadeInBitmapDisplayer(0))//是否图片加载好后渐入的动画时间
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
    }                     

public void displayImage(String uri,ImageView imageView,DisplayImageOptionsoptions) 第一个参数是String类型的URL

那我我该怎么在onBindViewHolder的 newses.get(position).getImages().get(0) 修改 为正确的
新手不太懂

阿神阿神2772 days ago442

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 17:41:00

    You are reporting a null pointer exception. Check if your newses is empty

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:41:00

    Use debug to debug and set an endpoint to see the value of newses. NullPointerException Speaking from experience, this thing has nothing to do with RecyclerView and everything to do with data.

    reply
    0
  • Cancelreply