当前位置: 首页 > 知识库问答 >
问题:

在Marshmallow的GridLayoutManager中遇到Android recyclerview问题

逄念
2023-03-14

嗨,我在Marshmallow的GridLayoutManager中使用recyclerview时遇到问题。我用一个片段列出一个列表,下面是代码

private RecyclerView mRecyclerView;
private RecyclerCategoryAdapter mListAdapter;
private GridLayoutManager mLayoutManager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_category, container, false);
    mSwipeLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_container);
    mRecyclerView = (RecyclerView) v.findViewById(R.id.category_recyclerview);
    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new GridLayoutManager(container.getContext(), 2);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if ( mListAdapter != null && mListAdapter.isPositionFooter(position) || mListAdapter.isPositionHeader(position)) {
                return mLayoutManager.getSpanCount();
            } else {
                return 1;
            }
        }
    });
    mRecyclerView.setLayoutManager(mLayoutManager);

    return v;
}

因此,如果列表中有页眉或页脚,我试图使内容填充屏幕的宽度,否则它应该是2列网格布局。在调用getSpanSize(int position)之后的第一条if语句中,应用程序崩溃。

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.memebox.us.adapter.recycler.RecyclerCategoryAdapter.isPositionHeader(int)' on a null object reference
   at com.memebox.us.fragment.CategoryFragment$1.getSpanSize(CategoryFragment.java:96)
   at android.support.v7.widget.GridLayoutManager$SpanSizeLookup.getSpanGroupIndex(GridLayoutManager.java:804)
   at android.support.v7.widget.GridLayoutManager.getSpanGroupIndex(GridLayoutManager.java:357)
   at android.support.v7.widget.GridLayoutManager.getRowCountForAccessibility(GridLayoutManager.java:128)
   at android.support.v7.widget.RecyclerView$LayoutManager.onInitializeAccessibilityNodeInfo(RecyclerView.java:7659)
   at android.support.v7.widget.RecyclerView$LayoutManager.onInitializeAccessibilityNodeInfo(RecyclerView.java:7619)
   at android.support.v7.widget.RecyclerViewAccessibilityDelegate.onInitializeAccessibilityNodeInfo(RecyclerViewAccessibilityDelegate.java:59)
   at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl$1.onInitializeAccessibilityNodeInfo(AccessibilityDelegateCompat.java:233)
   at android.support.v4.view.AccessibilityDelegateCompatJellyBean$1.onInitializeAccessibilityNodeInfo(AccessibilityDelegateCompatJellyBean.java:61)
   at android.view.View.onInitializeAccessibilityNodeInfo(View.java:6619)
   at android.view.View.createAccessibilityNodeInfoInternal(View.java:6580)
   at android.view.View$AccessibilityDelegate.createAccessibilityNodeInfo(View.java:23704)
   at android.view.View.createAccessibilityNodeInfo(View.java:6563)
   at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:145)
   at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:119)
   at android.view.View.onInitializeAccessibilityEventInternal(View.java:6517)
   at android.view.View$AccessibilityDelegate.onInitializeAccessibilityEvent(View.java:23616)
   at android.support.v4.view.AccessibilityDelegateCompatIcs.onInitializeAccessibilityEvent(AccessibilityDelegateCompatIcs.java:92)
   at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateIcsImpl.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:179)
   at android.support.v4.view.AccessibilityDelegateCompat.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:419)
   at android.support.v7.widget.RecyclerViewAccessibilityDelegate.onInitializeAccessibilityEvent(RecyclerViewAccessibilityDelegate.java:65)
   at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl$1.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:228)
   at android.support.v4.view.AccessibilityDelegateCompatJellyBean$1.onInitializeAccessibilityEvent(AccessibilityDelegateCompatJellyBean.java:56)
   at android.view.View.onInitializeAccessibilityEvent(View.java:6503)
   at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:6364)
   at android.view.View$AccessibilityDelegate.sendAccessibilityEventUnchecked(View.java:23555)
   at android.support.v4.view.AccessibilityDelegateCompatIcs.sendAccessibilityEventUnchecked(AccessibilityDelegateCompatIcs.java:117)
   at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateIcsImpl.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:210)
   at android.support.v4.view.AccessibilityDelegateCompat.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:358)
   at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl$1.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:255)
   at android.support.v4.view.AccessibilityDelegateCompatJellyBean$1.sendAccessibilityEventUnchecked(AccessibilityDelegateCompatJellyBean.java:82)
   at android.view.View.sendAccessibilityEventUnchecked(View.java:6347)
   at android.support.v7.widget.RecyclerView.sendAccessibilityEventUnchecked(RecyclerView.java:2649)
   at android.view.View$SendViewStateChangedAccessibilityEvent.run(View.java:23741)
   at android.view.View$SendViewStateChangedAccessibilityEvent.runOrPost(View.java:23774)
   at android.view.View.notifyViewAccessibilityStateChangedIfNeeded(View.java:9367)
   at android.view.View.onFocusChanged(View.java:6232)
   at android.view.View.clearFocusInternal(View.java:6124)
   at android.view.View.unFocus(View.java:6169)
   at android.view.ViewGroup.unFocus(ViewGroup.java:1010)
   at android.view.ViewGroup.unFocus(ViewGroup.java:1012)
   at android.view.ViewGroup.unFocus(ViewGroup.java:1012)
   at android.view.ViewGroup.unFocus(ViewGroup.java:1012)
   at android.view.ViewGroup.requestChildFocus(ViewGroup.java:750)
   at android.view.ViewGroup.requestChildFocus(ViewGroup.java:756)
   at android.view.ViewGroup.requestChildFocus(ViewGroup.java:756)
   at android.view.View.handleFocusGainInternal(View.java:5973)
   at android.view.ViewGroup.handleFocusGainInternal(ViewGroup.java:730)
   at android.view.View.requestFocusNoSearch(View.java:9053)
   at android.view.View.requestFocus(View.java:9032)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:3048)
   at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:3093)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:3049)
   at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:3093)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:3049)
   at android.view.View.requestFocus(View.java:8999)
   at android.view.View.requestFocus(View.java:8978)
   at android.support.v4.widget.DrawerLayout.dispatchOnDrawerOpened(DrawerLayout.java:750)
   at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:700)
   at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:1833)
   at android.support.v4.widget.ViewDragHelper.setDragState(ViewDragHelper.java:874)
   at android.support.v4.widget.ViewDragHelper$2.run(ViewDragHelper.java:335)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:158)
   at android.app.ActivityThread.main(ActivityThread.java:7229)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

下面是我使用的适配器代码

public boolean isPositionHeader(int position){
    return position == 0;
}

public boolean isPositionFooter(int position){
    return position == getItemCount() -1 ;
}

@Override
public int getItemCount() {
    return mProductList != null ? mProductList.size() + 2 : 0;
}

我试图总是在列表中添加另外两个项目作为页眉和页脚,并且总是在列表中添加两个视图(作为全宽度内容)

我想知道为什么消息中会出现“空对象引用上的RecyclerCategoryAdapter.isPositionHeader(int)”。

在调用方法“isPositionHeader(位置)”之前,我检查了Adapter的null,对于方法,isPositionHeader似乎没有任何null的可能性

这只发生在6.0和6.1版本的Android手机上。但我的手机从未出现过这种情况(我的手机是nexus 5x)。所以这并不总是发生

因为我刚刚开始将ListView替换为Recyclerview,所以我需要一个建议。有人能帮我解决这个问题吗?还是有人在为同样的问题而挣扎?

请问我是否需要附加更多代码

非常感谢。

共有1个答案

帅煌
2023-03-14

您对if语句有一个小问题。将其更改为以下内容:

if (mListAdapter != null && (mListAdapter.isPositionFooter(position) || mListAdapter.isPositionHeader(position)))
 类似资料:
  • 我最近在VSCODE中调试时遇到了一个很大的问题。我已经试着通过搜索网站和重新安装我的一些扩展来修复它。 它不会在调试控制台中显示结果,而是将以下输出写入终端:

  • 我正在尝试在最新的intellij(community edition)中使用Java9: IntelliJ IDEA 2016.3 Build#IC-163.7743.44,2016年11月17日构建jre:1.8.0_112-release-408-b2 x86 jvm:OpenJDK服务器VM由JetBrains S.r.o. 有人知道如何解决这个问题吗?还是我应该等到他们发布另一个版本?

  • 用的vue3 和element plus,在代码中 每次code1获取到的值都是上一次存入的值,网上搜说是因为异步的关系,具体应该怎么修改请大佬指导!

  • 问题内容: 我正在尝试玩 文件如此[处(1)])和here(2)所述。 (1)一个不好的例子: 第一种方法可行,但我不想依赖某些东西。第二个结果是仅播放了一些前导帧,听起来更像是单击。从ByteArrayInputStream播放时,不会是IO问题。 请与您分享为什么会发生这种情况。TIA。 问题答案: 我不确定为什么您链接的第二种方法会启动另一个线程;我相信音频还是会以自己的方式播放。剪辑播放完

  • 这是我的问题。我试过从绿鞋样品的视频示例。但我不能运行它,因为这个: 所以这给了我一个消息,我必须安装win32api gem。我尝试使用以下方法安装win32api: 错误:安装Win32-API时出错:错误:无法生成gem本机扩展。 正在检查strncpy_s()...不创建Makefile 结果记录到/home/msdc/.rvm/gems/rub-2.1.2/extensions/x86-

  • A)总结问题 在pom Maven中定义编译器版本有三种方法。我没有提到maven.compiler的版本、插件和版本(意味着maven构建成功),然后是如何选择编译器版本的,我没有提到版本 关于我试过的东西,请参阅b节 我定义了无效的Maven.Compiler.Release。然后它也起作用了 4.0.0 io.flowing.retail flow-retail-kafka-shipping