我有一个加载在Tab上的片段,然后加载RecyclerView并设置一个带有两个ViewHolder的适配器。屏幕上的数据通过CardViews显示在屏幕上。
顶部有一张卡,其余数据有多张卡。它的表示如下:
我的第一个也是最大的问题是它可以正常加载,如上图所示,但是当我尝试滚动时出现错误:
应用程序意外停止:线程[main,5,main]失败: java.lang.NullPointerException:尝试调用虚方法 '布尔 android.support.v7.widget.RecyclerView $ LayoutManager.canScrollHorizontally()' 在null对象引用 android.support.v7.widget.RecyclerView.computeHorizontalScrollOffset(RecyclerView.java:1548) 在android.view.View.canScrollHorizontally(View.java:12827)at android.support.v4.view.ViewCompatICS.canScrollHorizontally(ViewCompatICS.java:31) 在 android.support.v4.view.ViewCompat $ ICSViewCompatImpl.canScrollHorizontally(ViewCompat.java:1249) 在 android.support.v4.view.ViewCompat.canScrollHorizontally(ViewCompat.java:1684) 在android.support.v4.view.ViewPager.canScroll(ViewPager.java:2561) 在android.support.v4.view.ViewPager.canScroll(ViewPager.java:2552) 在android.support.v4.view.ViewPager.canScroll(ViewPager.java:2552) 在 android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1923) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1961)at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)at com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2625) 在 com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1770) 在android.app.Activity.dispatchTouchEvent(Activity.java:2742)处 android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) 在 android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) 在 com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2586) 在android.view.View.dispatchPointerEvent(View.java:8675)处 android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4129) 在 android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3995) 在 android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3550) 在 android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3603) 在 android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3569) 在 android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3686) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3577) 在 android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3743) 在 android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3550) 在 android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3603) 在 android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3569) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3577) 在 android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3550) 在 android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5813) 在 android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5787) 在 android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5758) 在 android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5903) 在 android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 在 android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(母语 方法)at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176) 在 android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5874) 在 android.view.ViewRootImpl $ ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5926) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) 在android.view.Choreographer.doCallbacks(Choreographer.java:580)at android.view.Choreographer.doFrame(Choreographer.java:548)at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) 在android.os.Handler.handleCallback(Handler.java:739)处 android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:135)at android.app.ActivityThread.main(ActivityThread.java:5294)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
我想问的第二件事是你是否认为这是实现这一目标的正确方法。我应该在RecyclerView中使用多个视图持有者吗?这会影响性能吗?
我的代码如下:
HomeFragment.java
public class HomeFragment extends Fragment{
private List<Favourite> favourites;
private RecyclerView rv;
FavouriteAdapter fa;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
// Get recycler view reference
rv = (RecyclerView) view.findViewById(R.id.favourite_card_rv);
// Set layout manager
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
// some data
// This only returns a list of objects with some data
initializeData();
// Create adapter
fa = new FavouriteAdapter(getActivity(), favourites);
// Set adapter
rv.setAdapter(fa);
// return
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
FavouriteAdapter.java
public class FavouriteAdapter extends RecyclerView.Adapter<FavouriteAdapter.MainViewHolder> {
List<Favourite> favourites;
Context context;
final int VIEW_TYPE_TOP = 0;
final int VIEW_TYPE_FAVOURITES = 1;
// Constructor
public FavouriteAdapter(Context context, List<Favourite> favourites) {
if (favourites == null) {
throw new IllegalArgumentException(
"favourites must not be null");
}
this.favourites = favourites;
this.context = context;
}
static class MainViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView videoName;
TextView videoUrl;
ImageView videoImage;
public MainViewHolder(View v) {
super(v);
cv = (CardView)itemView.findViewById(R.id.cv);
videoName = (TextView)itemView.findViewById(R.id.video_name);
videoUrl = (TextView)itemView.findViewById(R.id.video_url);
videoImage = (ImageView)itemView.findViewById(R.id.video_photo);
}
}
static class TopFavouriteViewHolder extends MainViewHolder{
TopFavouriteViewHolder(View itemView) {
super(itemView);
}
}
static class FavouriteViewHolder extends MainViewHolder {
FavouriteViewHolder(View itemView) {
super(itemView);
}
}
@Override
public int getItemCount() {
return favourites.size();
}
@Override
public int getItemViewType(int position) {
return (position == 0? VIEW_TYPE_TOP : VIEW_TYPE_FAVOURITES);
}
@Override
public MainViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view;
switch(viewType){
case 0:
view = LayoutInflater
.from(viewGroup.getContext())
.inflate(R.layout.top_card, viewGroup, false);
return new TopFavouriteViewHolder(view);
case 1:
view = LayoutInflater
.from(viewGroup.getContext())
.inflate(R.layout.favourite_card, viewGroup, false);
return new FavouriteViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(MainViewHolder holder, int i) {
holder.videoName.setText(favourites.get(i).name);
holder.videoUrl.setText(favourites.get(i).url);
Picasso.with(this.context).load(favourites.get(i).image).into(holder.videoImage);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
我在这里查看了很多主题并尝试了很多不同的组合,但似乎无法解决这个问题。
任何帮助都非常感激。
由于
答案 0 :(得分:3)
因此,您的层次结构中的某个地方似乎没有设置RecyclerView
的额外LayoutManager
。
如果查看堆栈跟踪的顶部:
Application has stopped unexpectedly: Thread[main,5,main] failed:
java.lang.NullPointerException: Attempt to invoke virtual method
'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()'
on a null object reference
at android.support.v7.widget.RecyclerView.computeHorizontalScrollOffset(RecyclerView.java:1548)
它表示在NullPointerException
实例上调用canScrollHorizontally()
时由RecyclerView$LayoutManager
导致崩溃。