我有一些水平网格排列的图像。我想使用键事件从左到右和从右到左移动网格。当我在网格中移动时,发生了两件事
选择第二个位置的项目。例如,我在位置0并且想要选择位置1处的下一个项目,但我的网格选择位置2项目并跳过位置1项目。
我得到空指针异常
if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
{
if (mCurrentSelectedPosition + 1 < getItemCount()) {
Log.e("current position1",Integer.toString(mCurrentSelectedPosition));
gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition);
gridHolder.itemView.setSelected(false);
notifyItemChanged(mCurrentSelectedPosition);
notifyDataSetChanged();
int lastItem = lm.findLastCompletelyVisibleItemPosition();
Log.e("last item",Integer.toString(lastItem));
if ((mCurrentSelectedPosition + 1) > lastItem) {
// Log.e("Is less", "true");
recyclerView.smoothScrollToPosition(mCurrentSelectedPosition + 1);
Log.e("current position2",Integer.toString(mCurrentSelectedPosition));
} else {
gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition + 1);
gridHolder.itemView.setSelected(true);
Log.e("current position3",Integer.toString(mCurrentSelectedPosition));
}
mCurrentSelectedPosition += 1;
Log.e("current position4",Integer.toString(mCurrentSelectedPosition));
}
return true;
}
滚动侦听器
gridRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int x, int y)
{
super.onScrolled(recyclerView, x, y);
try {
GridView_Holder gridHolder;
gridHolder= (GridView_Holder) recyclerView.findViewHolderForPosition(adapter.mCurrentSelectedPosition);
gridHolder.itemView.setSelected(true);
}catch(Exception e){
Log.e("exception",e.getMessage());
}
}
这是我的logcat
java.lang.NullPointerException: Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$ViewHolder.itemView' on a null object reference
at com.winettv.recylcerviewwithdpad.GridView_Recycler_Adapter$1.onKey(GridView_Recycler_Adapter.java:193)
at android.view.View.dispatchKeyEvent(View.java:9230)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1635)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:379)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2395)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1727)
at android.app.Activity.dispatchKeyEvent(Activity.java:2725)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:543)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:315)
答案 0 :(得分:3)
在代码 gridHolder.itemView.setSelected(true); 中 gridHolder为空。
更改此类代码并尝试。
if ((mCurrentSelectedPosition + 1) > lastItem) {
// Log.e("Is less", "true");
recyclerView.smoothScrollToPosition(mCurrentSelectedPosition);
Log.e("current position2",Integer.toString(mCurrentSelectedPosition));
} else {
gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition);
gridHolder.itemView.setSelected(true);
Log.e("current position3",Integer.toString(mCurrentSelectedPosition));
}
崩溃的原因。
答案 1 :(得分:2)
当我在滚动期间使用自定义翻译开发库时,我遇到了同样的问题。只有在快速滚动时,当我尝试从List访问View元素时,这是null,这里是NPE。
所以有一个非常简单的解决方案只是跳过元素,这是空的,并继续新的元素。就我而言,它是这样的:
// Other init....
for (int i = 0; i < MainListView.getChildCount; i++) {
if (MainListView.getChildAt(i) == null)
continue;
// Continue base behavior
}
这种情况发生在ScrollView(它是RecycleView,ListView等的父级)的基础上。当我们尝试访问正在重绘的View时。无论如何只需简单的Null检查就可以解决您的问题。
更新1:
同样基于缓存查看RecyclerView的布局,您还需要另一个选项。见下文。
gridRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int x, int y)
{
super.onScrolled(recyclerView, x, y);
GridView_Holder gridHolder;
gridHolder= (GridView_Holder) recyclerView.findViewHolderForPosition(adapter.mCurrentSelectedPosition);
if (gridHolder != null && gridHolder.itemView != null) {
gridHolder.itemView.setSelected(true);
}
}
}
答案 2 :(得分:1)
根据错误,很明显
(GridView_Holder)mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition);
为空。这就是崩溃的原因。
您可以参考以下链接
FindViewHolderForPosition return null when card's not in vision
RecyclerView - findViewHolderForPosition always returns null when onBindViewHolder is not called