在OREO及更高版本中RecyclerView ItemView崩溃

时间:2019-08-15 11:20:44

标签: android android-recyclerview nullpointerexception recycler-adapter

  

我的应用程序在低端设备上运行正常。它仅在OREO及更高版本的设备上崩溃。我无法弄清楚为什么itemView崩溃。

堆栈跟踪

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$x.ItemView' on a null object reference
       at com.Explore.ExploreFragment$1.run(ExploreFragment.java:12)
       at android.os.Handler.handleCallback + 790(Handler.java:790)
       at android.os.Handler.dispatchMessage + 99(Handler.java:99)
       at android.os.Looper.loop + 164(Looper.java:164)
       at android.app.ActivityThread.main + 7000(ActivityThread.java:7000)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 441(RuntimeInit.java:441)
       at com.android.internal.os.ZygoteInit.main + 1408(ZygoteInit.java:1408)

ViewHolder

public class ExploreVH extends RecyclerView.ViewHolder implements View.OnClickListener {

private OnExploreItemClick onExploreItemClick;

@BindView(R.id.lbl_heading_dynamic_view)
TypefaceTextView heading;
@BindView(R.id.resource_image)
ImageView resource_image ;
@BindView(R.id.tv_lbl_dynamic_view)
TypefaceTextView nextText;
@BindView(R.id.arrow_image_usage)
ImageView arrowIv;

public ExploreVH(@NonNull View itemView,OnExploreItemClick onExploreItemClick){
    super(itemView);
    this.onExploreItemClick = onExploreItemClick;

    ButterKnife.bind(this, itemView);
    itemView.setOnClickListener(this);
}

public void bindView(ExploreBO exploreBO){
    String appVersion = BuildConfig.VERSION_NAME;
    if(exploreBO != null && exploreBO.isHomeBannerEnabled() && exploreBO.getIncludeVersionNumbers() != null
        && exploreBO.getIncludeVersionNumbers().contains(appVersion)){

        try {

            heading.setText(exploreBO.getHomeBannerHeading());

            if(exploreBO.getHomeBannerBtnText() != null){
                if(exploreBO.getHomeBannerBtnText().trim().isEmpty() || exploreBO.getHomeBannerBtnText().equalsIgnoreCase(" ")){
                    nextText.setVisibility(View.GONE);
                    arrowIv.setVisibility(View.GONE);
                }
                nextText.setText(exploreBO.getHomeBannerBtnText());
            }
            if(exploreBO.getButtonColor() != null && !exploreBO.getButtonColor().isEmpty())
                nextText.setTextColor(Color.parseColor(exploreBO.getButtonColor()));

            Picasso.get().load(exploreBO.getHomeBannerImage()).placeholder(R.drawable.large_placeholder).into(resource_image);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Override
public void onClick(View view) {
    if(onExploreItemClick != null)
        onExploreItemClick.onItemClicked(getAdapterPosition());
}

}

适配器

public class ExploreListAdapter extends RecyclerView.Adapter<ExploreVH> {

    private List<ExploreBO> items;
    private OnExploreItemClick onExploreItemClick;

    public ExploreListAdapter(List<ExploreBO> items, OnExploreItemClick onExploreItemClick) {
        this.items = items;
        this.onExploreItemClick = onExploreItemClick;
    }

    @NonNull
    @Override
    public ExploreVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_explore,viewGroup,false);
        return new ExploreVH(mView,onExploreItemClick);
    }

    @Override
    public void onBindViewHolder(@NonNull ExploreVH exploreVH, int i) {
        exploreVH.bindView(items.get(i));
    }

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

修改

private void clickAtIndex(int position) {
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            mRecyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();
        }
    });
}

找到解决方案

private void clickAtIndex(int position) {
        try {
            mRecyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(mRecyclerView.findViewHolderForAdapterPosition(position) != null )
                        mRecyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();
                }
            },50);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

0 个答案:

没有答案