gpt4 book ai didi

android - 在 fragment 内滚动时出现 RecyclerView 错误

转载 作者:行者123 更新时间:2023-11-29 17:24:33 24 4
gpt4 key购买 nike

我有一个加载到 Tab 上的 fragment ,然后加载 RecyclerView 并设置一个带有两个 ViewHolder 的适配器。屏幕上的数据通过CardViews显示在屏幕上。

一张卡片用于顶部数据,多张卡片用于其余数据。其表示如下:

enter image description here

我的第一个也是最大的问题是它可以正常加载,如上图所示,但是当我尝试滚动时出现错误:

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) at android.view.View.canScrollHorizontally(View.java:12827) at android.support.v4.view.ViewCompatICS.canScrollHorizontally(ViewCompatICS.java:31) at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollHorizontally(ViewCompat.java:1249) at android.support.v4.view.ViewCompat.canScrollHorizontally(ViewCompat.java:1684) at android.support.v4.view.ViewPager.canScroll(ViewPager.java:2561) at android.support.v4.view.ViewPager.canScroll(ViewPager.java:2552) at android.support.v4.view.ViewPager.canScroll(ViewPager.java:2552) at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1923) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1961) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2625) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1770) at android.app.Activity.dispatchTouchEvent(Activity.java:2742) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2586) at android.view.View.dispatchPointerEvent(View.java:8675) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4129) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3995) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3686) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3743) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5813) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5787) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5758) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5903) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176) at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5874) at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5926) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:548) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) at android.os.Handler.handleCallback(Handler.java:739) at 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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

我想问的第二件事是您认为这是否是实现它的正确方法。我应该在 RecyclerView 中使用多个 View 持有者吗?这会影响性能吗?

我的代码如下:

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);
}
}

我在这里查看了很多线程并尝试了很多不同的组合,但似乎无法解决这个问题。

非常感谢任何帮助。

谢谢

最佳答案

因此看起来您在层次结构中的某处有一个额外的 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)

它表示在 RecyclerView$LayoutManager 实例上调用 canScrollHorizo​​ntally() 时由于 NullPointerException 而崩溃。

关于android - 在 fragment 内滚动时出现 RecyclerView 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35140560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com