【问题标题】:Fragment and RecyclerView errorRecyclerView$ViewHolder.shouldIgnore()Fragment 和 RecyclerView errorRecyclerView$ViewHolder.shouldIgnore()
【发布时间】:2019-03-20 07:18:29
【问题描述】:

我曾经有一个显示 recyclerView 的主要活动,它运行良好。然后,我决定使用 recyclerView 而不仅仅是 recyclerView 创建一个片段列表,以允许在列表中显示一些很酷的项目。

适配器类

public class BeersAdapter extends RecyclerView.Adapter<BeersAdapter.BeersViewHolder> {
private List<Beer> beers;
    public static class BeersViewHolder extends RecyclerView.ViewHolder {
        public ImageView beerImage;
        public  TextView beerName;
        public  TextView beerDesc;
        public  BeersViewHolder (View itemView){
            super(itemView);
            beerImage = itemView.findViewById(R.id.beerImageLayout);
            beerName = itemView.findViewById(R.id.beerNameLayout);
            beerDesc = itemView.findViewById(R.id.beerDescLayout);
        }
    }
    public BeersAdapter(List<Beer> b){
        beers = b;
    }
    @Override
    public BeersAdapter.BeersViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.beer, parent,false);
        return new BeersViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(BeersViewHolder holder, int pos) {
        holder.beerName.setText(beers.get(pos).getName());
        holder.beerDesc.setText(beers.get(pos).getShortDescription());

    }

    @Override
    public int getItemCount() {
    return beers.size();
    }
}

片段类中的onCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_mainbeer_list, container, false);

    RecyclerView rV =  view.findViewById(R.id.BeerRV);
    rV.setHasFixedSize(true);

    rV.setLayoutManager(new LinearLayoutManager(getActivity()));

    beers = Beer.getDummyBeers();
    BeersAdapter mAdapter = new BeersAdapter(beers);

    rV.setAdapter(mAdapter);

    rV.setItemAnimator(new DefaultItemAnimator());

    return view;
}

我知道如果我 return view = inflater.inflate(R.layout.fragment_mainbeer_list, container, false); 未使用它不会给出错误,但也没有任何改变。我的意思是我可以在 onCreateView 上运行代码,但是如果我返回我使用的视图(据我所知,它不应该被更改)它会导致错误。

我在运行调试器时遇到的错误

java.lang.NullPointerException:尝试调用虚拟方法 '布尔值 android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' 空对象引用

编辑: 我知道 NullPointer 是什么。我只是不知道为什么我得到它。因为我的应用程序运行正常,顺序相同,唯一不同的是我现在使用的是片段,并在片段而不是 MainActivity 中创建 RV。基于this post

编辑 2: fragment_mainbeer_list.xml 的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView         
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/BeerRV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

对于片段所在的main_activy.xml

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/ListFragment"
        android:name="com.example.beers_fragmnet.MainBeerFragment"
        android:layout_width="215dp"
        android:layout_height="328dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:layout="@layout/beer" />
</android.support.constraint.ConstraintLayout>

附加信息

该应用程序仅显示啤酒列表,在单击啤酒时尝试添加片段之前,它会打开详细活动并显示有关啤酒的信息。

我现在想让它在平板电脑上的工作方式与许多电子邮件应用程序的工作方式相同,当您单击电子邮件时,它不会打开新活动,而是在同一活动中打开电子邮件(以防此处出现错误解释是我想说的photo

【问题讨论】:

  • 在将 Adapter 设置为 RecyclerView 后尝试移动 rV.setHasFixedSize(true);
  • @Zoe 我可以看到这是一个 NullPointerException,我知道这意味着什么(我是 C/C++ 的主要程序)。我只是不知道为什么会得到它,尤其是当 onCreateView 中的相同代码适用于 MainActivty 但现在抛出 NullPointer 时。我显然错过了一些东西,我花了几个小时试图看看是否有人有类似的问题,但我找不到任何东西,所以我在这里
  • @ישואוהבאותך 我试着移动它,并把它注释掉(因为这段代码只是为了提高性能,所以它可以计算 RV 的最大尺寸)。不过谢谢你的建议

标签: android android-fragments android-recyclerview


【解决方案1】:

编辑:你能提供你的 XML 代码吗?

编辑 2:尝试使用 FrameLayout 而不是 fragment

看看this answer

【讨论】:

  • 据我了解 XML 代码,我在 fragment_mainbeer_list.xml 中有 BeerRV 但是我刚刚创建了 Fragment_mainbeer_list 并在其中添加了我以前的 RV 中的 xml 代码。我已经添加了 XML 代码,感谢您抽出宝贵时间帮助我
  • 我刚刚测试了你的代码,它工作正常。哪一行代码产生了这个错误?
  • 我收到一条很长的错误消息,但不是我编写的任何代码。 Here is the error code
  • 尝试使用
  • 是的,这行得通。但是FrameLayout和fragment有什么区别呢?我还想知道为什么片段不起作用
猜你喜欢
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
相关资源
最近更新 更多