一个垂直RecyclerView嵌套两个横向RecyclerView,第一个可以正常滑动,第二个只要滑动到第四个item就立马包空指针崩溃

BiteMan 发布于 2017/05/10 21:27
阅读 4K+
收藏 0

两个横向滑动的RecyclerView不会在同时显示,第一个可以完好的显示并且滑动顺畅,第二个却每次滑动到第四个item时报出holder中的对象为空.我把holder.img注销掉,只设置文字,也是如此.

垂直RecyclerView的Adapter

public class JingAdapter extends RecyclerView.Adapter<JingAdapter.HeiHei> {
private Context context;
private List<JingBean.ItemListBean> list;
private String type;
private final Typeface typeface;
private ScollAdapter adapter;

public JingAdapter(Context context) {
    this.context = context.getApplicationContext();
    list = new ArrayList<>();
    typeface = Typeface.createFromAsset(context.getAssets(), "Lobster-1.4.otf");

}

@Override
public HeiHei onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;
    switch (viewType) {
        case 1:
            view = LayoutInflater.from(context).inflate(R.layout.item_jing_header,parent,false);
            break;
        case 2:
            view = LayoutInflater.from(context).inflate(R.layout.itme_jing,parent,false);
            break;
        case 3:
            view = LayoutInflater.from(context).inflate(R.layout.item_other,parent,false);
            break;
        case 4:
            view = LayoutInflater.from(context).inflate(R.layout.item_many,parent,false);
            break;
    }
    return new HeiHei(view);
}

@Override
public void onBindViewHolder(HeiHei holder, int position) {
    if (position == 0) {
        holder.headerDate.setTypeface(typeface);
        holder.headerDate.setText(DateUtile.getInstance().getDate());
    }else {
        JingBean.ItemListBean bean = list.get(position-1);
        String type = bean.getType();
        if ("video".equals(type)){
            holder.title.setText(bean.getData().getTitle());
            holder.img.setImageURI(bean.getData().getCover().getFeed());
        }else if ("banner".equals(type)){
            holder.img.setImageURI(bean.getData().getImage());
        }else if ("horizontalScrollCard".equals(type)){
            holder.img.setImageURI(bean.getData().getItemList().get(0).getData().getImage());
        }else if ("textCard".equals(type)){
            holder.otherTitle.setText(bean.getData().getText());
            if ("header1".equals(bean.getData().getType())){
                holder.bt.setVisibility(View.GONE);
            }
        }else if ("videoCollectionWithCover".equals(type) || "videoCollectionOfFollow".equals(type)){
            holder.manyImg.setImageURI(bean.getData().getHeader().getCover());
            adapter = new ScollAdapter(context, bean.getData().getItemList());
            holder.manyList.setAdapter(adapter);
        }
    }
}

@Override
public int getItemViewType(int position) {
    if(position >= 1){
        type = list.get(position-1).getType();
        if ("video".equals(type) || "banner".equals(type) || "horizontalScrollCard".equals(type)){
            return 2;
        }else if ("textCard".equals(type)){
            return 3;
        }else if ("videoCollectionWithCover".equals(type) || "videoCollectionOfFollow".equals(type)){
            return 4;
        }
    }
    return 1;
}

@Override
public int getItemCount() {
    return list.size()+1;
}

public class HeiHei extends RecyclerView.ViewHolder{

    private final TextView title;
    private final TextView headerDate;
    private final TextView otherTitle;
    private final SimpleDraweeView img;
    private final SimpleDraweeView manyImg;
    private final ImageView bt;
    private final RecyclerView manyList;

    public HeiHei(View itemView) {
        super(itemView);
        title = (TextView)itemView.findViewById(R.id.jing_title);
        otherTitle = (TextView)itemView.findViewById(R.id.other_title);
        headerDate = (TextView)itemView.findViewById(R.id.header_date);
        img = (SimpleDraweeView)itemView.findViewById(R.id.jing_img);
        manyImg = (SimpleDraweeView)itemView.findViewById(R.id.many_img);
        bt = (ImageView)itemView.findViewById(R.id.other_bt);
        manyList = (RecyclerView)itemView.findViewById(R.id.many_list);
        if (manyList != null){
            manyList.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
            manyList.setAdapter(null);
        }
    }
}

public void addAll(JingBean.ItemListBean jingList){
    list.add(jingList);
    notifyDataSetChanged();
}

}

水平滑动的RecyclerView的Adapter

public class ScollAdapter extends RecyclerView.Adapter<ScollAdapter.ScollHolder> {
private List<JingBean.ItemListBean.DataBean.ItemListBeanX> list;
private Context context;
public ScollAdapter(Context context,List<JingBean.ItemListBean.DataBean.ItemListBeanX> list) {
    this.list = list;
    this.context = context.getApplicationContext();
}

@Override
public ScollHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == 1){
        view = LayoutInflater.from(context).inflate(R.layout.item_scoll, parent, false);
    }else {
        view = LayoutInflater.from(context).inflate(R.layout.item_last,parent,false);
    }
    return new ScollHolder(view);
}


@Override
public void onBindViewHolder(ScollHolder holder, int position) {
    JingBean.ItemListBean.DataBean.ItemListBeanX.DataBeanX dataBeanX = list.get(position).getData();
    if ("ActionCard".equals(dataBeanX.getDataType())){
        holder.more.setText(dataBeanX.getText());
    }else {
        holder.img.setImageURI(Uri.parse(dataBeanX.getCover().getFeed()));
        holder.title.setText(dataBeanX.getTitle());
        holder.catagory.setText(dataBeanX.getCategory()+dataBeanX.getDuration());
    }
}

@Override
public int getItemViewType(int position) {
    if (position == list.size()-1){
        return 2;
    }
    return 1;
}

@Override
public int getItemCount() {
    return list.size();
}

public class ScollHolder extends RecyclerView.ViewHolder{

    private final SimpleDraweeView img;
    private final TextView title;
    private final TextView catagory;
    private final TextView more;

    public ScollHolder(View itemView) {
        super(itemView);
        img = (SimpleDraweeView)itemView.findViewById(R.id.scoll_img);
        title = (TextView)itemView.findViewById(R.id.scoll_title);
        catagory = (TextView)itemView.findViewById(R.id.scoll_class);
        more = (TextView)itemView.findViewById(R.id.scoll_more);
    }
}
}

错误信息:

AndroidRuntime: FATAL EXCEPTION: main
     Process: poul.com.kaiyan, PID: 16949
     java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.drawee.view.SimpleDraweeView.setImageURI(android.net.Uri)' on a null object reference
     at poul.com.kaiyan.kai_adapter.ScollAdapter.onBindViewHolder(ScollAdapter.java:50)
     at poul.com.kaiyan.kai_adapter.ScollAdapter.onBindViewHolder(ScollAdapter.java:24)
     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6308)
     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6341)
     at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5287)
     at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5550)
     at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:270)
     at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:324)
     at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:337)
     at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:344)
     at android.support.v7.widget.GapWorker.run(GapWorker.java:370)
     at android.os.Handler.handleCallback(Handler.java:815)
     at android.os.Handler.dispatchMessage(Handler.java:104)
     at android.os.Looper.loop(Looper.java:194)
     at android.app.ActivityThread.main(ActivityThread.java:5562)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763
加载中
0
小轩17
小轩17
childview is null. please checkout your childview draw before you ontunch you can debug
0
BiteMan
BiteMan

谢谢谢谢,我已经解决了,非常感谢

返回顶部
顶部