[CymChad/BaseRecyclerViewAdapterHelper]使用BaseMultiItemQuickAdapter 布局包含View会报空指针异常

2024-08-08 153 views
0

版本:2.9.12

因为我平时习惯画线的时候使用View来画,如: <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/black"/>

这样的布局在使用多布局的时候报如下错误了(换成其他布局则不会有这种问题,希望楼主能解答下):

04-19 09:53:26.259 27123-27123/zz.mk.zgarden E/AndroidRuntime: FATAL EXCEPTION: main Process: zz.mk.zgarden, PID: 27123 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:722) at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) at android.view.LayoutInflater.rInflate(LayoutInflater.java:821) at android.view.LayoutInflater.inflate(LayoutInflater.java:511) at android.view.LayoutInflater.inflate(LayoutInflater.java:415) at zz.mk.baserecycleview.BaseQuickAdapter.getItemView(BaseQuickAdapter.java:1322) at zz.mk.baserecycleview.BaseQuickAdapter.createBaseViewHolder(BaseQuickAdapter.java:846) at zz.mk.baserecycleview.BaseMultiItemQuickAdapter.onCreateDefViewHolder(BaseMultiItemQuickAdapter.java:48) at zz.mk.baserecycleview.BaseQuickAdapter.onCreateViewHolder(BaseQuickAdapter.java:671) at zz.mk.baserecycleview.BaseQuickAdapter.onCreateViewHolder(BaseQuickAdapter.java:65) at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6367) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5555) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at zz.mk.zgarden.common.view.ptrefresh.PtrFrameLayout.layoutChildren(PtrFrameLayout.java:289) at zz.mk.zgarden.common.view.ptrefresh.PtrFrameLayout.onLayout(PtrFrameLayout.java:258) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) at android.view.View.layout(View.java:16711) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.w

回答

3

你布局对应的itemtype没有初始化?

3

@CymChad 不好意思,布局文件没有显示出来。 初始化什么的都做完了,就是下面这个布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dip"
    android:background="@color/white"
    android:gravity="center_vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:drawablePadding="6dip"
        android:gravity="center_vertical"
        android:textColor="@color/black"/>

</LinearLayout>

<View android:layout_width="match_parent" android:layout_height="1dp"
android:background="@color/black"/>

如果换成下面这样就会报错,差别就是用view画了一条线,如果我换成用textview画线则没有问题:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dip"
    android:background="@color/white"
    android:gravity="center_vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:drawablePadding="6dip"
        android:gravity="center_vertical"
        android:textColor="@color/black"/>

</LinearLayout>

4

@CymChad 布局文件贴出来显示不知道为什么会有问题。。。这里排除布局写错的情况,两个布局唯一差别就是正常显示那个布局少了view这个控件。另外如果我把view换成其他的例如textview来画线的话则没有问题。

4

布局加载报错,如果你不用多布局就不会报错吗?

9

@CymChad 嗯,直接继承BaseQuickAdapter的画没有问题。

2

@CymChad 这里还有个关于headview的问题也希望楼主能解答下: 当我下拉刷新动态添加headview的时候,如果先addheadview 然后再setNewData的话,第一次自动加载的时候没啥问题,当我再手动下拉刷新的时候就会发现只显示headview,下面的list不会显示。只有先setNewData然后addheadview才会正常显示。

0

View 的问题。。最底下别用View

5

@mackkill 我明天试下demo

1

@mackkill addHeaderView只需要添加一次吧?

7

我在demo测试了一下先addheadview然后setNewData没有出现你说的问题,你能提供有问题的demo给我吗?我这边调试一下。

4

我在demo的BaseMultiItemQuickAdapter的item布局中添加了你的

<View android:layout_width="match_parent" android:layout_height="1dp"
android:background="@color/black"/>

也是正常运行。

6

你检查一下你的代码,如果还是有问题,可以发个demo地址给我,我这边调试一下。

1

Relativelayout 里 在底部加一个贴底部的view就报这个错,删除这个view就好了,确实是个bug