CymChad / BaseRecyclerViewAdapterHelper

BRVAH:Powerful and flexible RecyclerAdapter
http://www.recyclerview.org/
MIT License
24.33k stars 5.16k forks source link

InvocationTargetException 怎么解决? #1702

Closed super-hu closed 6 years ago

super-hu commented 7 years ago

只要自定义BaseViewHolder的有设置头部尾部或者加载更多就会错误,看了其他的issues,没看懂怎么解决

11-03 17:37:26.271 32597-32597/? W/System.err: java.lang.reflect.InvocationTargetException
11-03 17:37:26.281 32597-32597/? W/System.err:     at java.lang.reflect.Constructor.newInstance(Native Method)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.chad.library.adapter.base.BaseQuickAdapter.createGenericKInstance(BaseQuickAdapter.java:1032)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.chad.library.adapter.base.BaseQuickAdapter.createBaseViewHolder(BaseQuickAdapter.java:1012)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.chad.library.adapter.base.BaseQuickAdapter.getLoadingView(BaseQuickAdapter.java:772)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.chad.library.adapter.base.BaseQuickAdapter.onCreateViewHolder(BaseQuickAdapter.java:750)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.chad.library.adapter.base.BaseQuickAdapter.onCreateViewHolder(BaseQuickAdapter.java:68)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.RecyclerView$a.createViewHolder(RecyclerView.java:6367)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.RecyclerView$n.a(RecyclerView.java:5555)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.RecyclerView$n.a(RecyclerView.java:5440)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.RecyclerView$n.b(RecyclerView.java:5436)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.LinearLayoutManager$c.a(LinearLayoutManager.java:2224)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.LinearLayoutManager.a(LinearLayoutManager.java:1551)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.LinearLayoutManager.a(LinearLayoutManager.java:1511)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.LinearLayoutManager.c(LinearLayoutManager.java:595)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.RecyclerView.N(RecyclerView.java:3583)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1420)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:652)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.View.measure(View.java:20151)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.Choreographer.doCallbacks(Choreographer.java:695)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.Choreographer.doFrame(Choreographer.java:631)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.os.Looper.loop(Looper.java:158)
11-03 17:37:26.281 32597-32597/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7237)
11-03 17:37:26.281 32597-32597/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
11-03 17:37:26.281 32597-32597/? W/System.err: Caused by: java.lang.IllegalStateException: Required view 'group_title_tv' with ID 2131624257 for field 'groupTitleTv' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
11-03 17:37:26.281 32597-32597/? W/System.err:     at butterknife.a.b.a(Utils.java:92)
11-03 17:37:26.281 32597-32597/? W/System.err:     at butterknife.a.b.a(Utils.java:104)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.zjcs.group.ui.personal.adapter.GroupListAdapter$ItemHodel_ViewBinding.<init>(GroupListAdapter$ItemHodel_ViewBinding.java:22)
11-03 17:37:26.281 32597-32597/? W/System.err:     at java.lang.reflect.Constructor.newInstance(Native Method)
11-03 17:37:26.281 32597-32597/? W/System.err:     at butterknife.ButterKnife.b(ButterKnife.java:199)
11-03 17:37:26.281 32597-32597/? W/System.err:     at butterknife.ButterKnife.a(ButterKnife.java:172)
11-03 17:37:26.281 32597-32597/? W/System.err:     at com.zjcs.group.ui.personal.adapter.GroupListAdapter$ItemHodel.<init>(GroupListAdapter.java:71)
11-03 17:37:26.281 32597-32597/? W/System.err:  ... 73 more
super-hu commented 7 years ago

都没人维护吗? 我看了源码,不知道为什么所有的item都是走K这个泛型,在loading或者底部布局都返回K。 在不改动源码情况下,在自定义ViewHolder下现在只能K继续返回BaseViewHolder,去重写onCreateDefViewHolder返回自定义的,这样其他的item就不会去找自定义的ViewHoder。 暂时是这样解决的。

CymChad commented 7 years ago

收到,最近大家都比较忙,所以回复的不及时。

CymChad commented 7 years ago

你的版本是多少?

AllenCoder commented 7 years ago
Caused by: java.lang.IllegalStateException: Required view 'group_title_tv' with ID 2131624257 for field 'groupTitleTv' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.`

自定义viewholder里面你的itemView和数据类型绑定应该有问题,提示 itemView的id找不到,请仔细检查数据源和视图view是否绑定正确,如有可能,提供复现demo 以供排除问题

super-hu commented 7 years ago

@CymChad 最新的 @AllenCoder 不知道是不是用了ButterKnife框架的问题冲突? 我修改Demo的PullToRefreshAdapter,改成自定义的ViewHolder,里面用ButterKnife去注入控件,就会报上述的错误。 用系统的findViewById反而没事,当初开发的时候忘记有没有测试findViewById的问题出现,但是现在ButterKnife是必复现。

是不是和这个问题类似?#598

修改的code:


public class PullToRefreshAdapter extends BaseQuickAdapter<Status, PullToRefreshAdapter.MyViewHolder> {

    public PullToRefreshAdapter() {
        super(R.layout.layout_animation, DataServer.getSampleData(10));
    }

    @Override
    protected void convert(MyViewHolder helper, Status item) {
        switch (helper.getLayoutPosition() %
                3) {
            case 0:
                helper.img.setImageResource(R.mipmap.animation_img1);
                break;
            case 1:
                helper.img.setImageResource(R.mipmap.animation_img2);
                break;
            case 2:
                helper.img.setImageResource(R.mipmap.animation_img3);
                break;
        }
        helper.tweetName.setText("Rio");
        String msg = "\"He was one of Australia's most of distinguished artistes, renowned for his portraits\"";
        helper.tweetText.setText(SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create());
        helper.tweetText.setMovementMethod(LinkMovementMethod.getInstance());
    }

    public static class MyViewHolder extends BaseViewHolder {

        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.tweetName)
        TextView tweetName;
        @BindView(R.id.tweetText)
        TextView tweetText;
        @BindView(R.id.tweetDate)
        TextView tweetDate;
        @BindView(R.id.card_view)
        CardView cardView;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
//            img= (ImageView) view.findViewById(R.id.img);
//            tweetName= (TextView) view.findViewById(R.id.tweetName);
//            tweetText= (TextView) view.findViewById(R.id.tweetText);
        }

    }

    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            ToastUtils.showShortToast("事件触发了 landscapes and nedes");
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Utils.getContext().getResources().getColor(R.color.clickspan_color));
            ds.setUnderlineText(true);
        }
    };

}
AllenCoder commented 6 years ago

参考链接 https://github.com/JakeWharton/butterknife/issues/230

With this you'll probably see that it cannot find the generated class. This means one of two things:

1. Annotation processing is not running. If you are building from an IDE there are instructions on the website at the bottom.
2. ProGuard is stripping or renaming the class. There are instructions on the website for preventing that.
AllenCoder commented 6 years ago
  1. 自定义ViewHolder使用静态内部类
  2. 开启混淆规则要加入BRVAH的混淆规则
  3. 自定义BaseViewHolder的有设置头部尾部或者加载更多 对应的布局文件不包含ItemView内部的viewid,ButterKnife 在findViewById 在头布局或者加载更多的布局时,会找不到控件id而发生崩溃
super-hu commented 6 years ago

butterknife只要是public 就可以了。。。你测试过没有? 上面给的代码也是public static MyViewHolder .. 混淆当然是加了,况且我跑的是debug,不影响。。。 看我第二条回复的,是可以解决,但是你们文档提供的方式不能处理这种异常。不会奔溃,捕获了异常。

问题就出在你说的第三条,底部头部等布局都去走Item的Holder,才会出现的、

CymChad commented 6 years ago

如果是第三条的原因是不是可以判断下item的type再走butterknife加载方法?这样是不是可以解决?

super-hu commented 6 years ago

应该是吧,但是需要再初始化布局的时候就需要去判断type,如果大家用到butterknife,可以先按这样的方式来写,目前没发现其他问题。我暂时先关闭吧。

_15099325847267 _15099326094558

CymChad commented 6 years ago

多谢提供解决方案 @hcadoid