在 Marshmallow 中使用 GridLayoutManager 遇到 Android recyclerview 问题
Having trouble with Android recyclerview with GridLayoutManager in Marshmallow
您好,我在 Marshmallow 中将 recyclerview 与 GridLayoutManager 结合使用时遇到问题。我用一个片段来制作一个列表,这是代码
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.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.onInitializeAccessibilityNodeInfo(AccessibilityDelegateCompat.java:233)
at android.support.v4.view.AccessibilityDelegateCompatJellyBean.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.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:228)
at android.support.v4.view.AccessibilityDelegateCompatJellyBean.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.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:255)
at android.support.v4.view.AccessibilityDelegateCompatJellyBean.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.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)' on a null object reference"在邮件中。
我在调用方法 "isPositionHeader(position)" 之前检查了 Adapter 是否为 null,而方法 isPositionHeader 似乎没有任何可能为 null
它只发生在 Android phone 的 6.0 和 6.1 版本上。但我的 phone 从来没有发生过(我的 phone 是 nexus 5x)。所以它并不总是发生
由于我刚刚开始将 ListView 替换为 Recyclerview,所以我需要一个建议。
谁能帮我解决这个问题?或者有没有人遇到同样的问题?
请问我是否需要附加更多代码
谢谢
您的 if 语句有一个小问题。将其更改为以下内容:
if (mListAdapter != null && (mListAdapter.isPositionFooter(position) || mListAdapter.isPositionHeader(position)))
您好,我在 Marshmallow 中将 recyclerview 与 GridLayoutManager 结合使用时遇到问题。我用一个片段来制作一个列表,这是代码
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.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.onInitializeAccessibilityNodeInfo(AccessibilityDelegateCompat.java:233)
at android.support.v4.view.AccessibilityDelegateCompatJellyBean.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.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:228)
at android.support.v4.view.AccessibilityDelegateCompatJellyBean.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.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:255)
at android.support.v4.view.AccessibilityDelegateCompatJellyBean.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.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)' on a null object reference"在邮件中。
我在调用方法 "isPositionHeader(position)" 之前检查了 Adapter 是否为 null,而方法 isPositionHeader 似乎没有任何可能为 null
它只发生在 Android phone 的 6.0 和 6.1 版本上。但我的 phone 从来没有发生过(我的 phone 是 nexus 5x)。所以它并不总是发生
由于我刚刚开始将 ListView 替换为 Recyclerview,所以我需要一个建议。 谁能帮我解决这个问题?或者有没有人遇到同样的问题?
请问我是否需要附加更多代码
谢谢
您的 if 语句有一个小问题。将其更改为以下内容:
if (mListAdapter != null && (mListAdapter.isPositionFooter(position) || mListAdapter.isPositionHeader(position)))