& quot; RecyclerView.setHasFixedSize(boolean)'在空对象引用上".Recyclerview变成片段 [英] "RecyclerView.setHasFixedSize(boolean)' on a null object reference" Recyclerview into fragment

查看:104
本文介绍了& quot; RecyclerView.setHasFixedSize(boolean)'在空对象引用上".Recyclerview变成片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选项卡式活动,我想在第一个片段(Tab1History)中显示Recyclerview和Cardview.我已经为CardView创建了一个XML文件,并在片段XML中插入了Recyclerview.我还创建了适配器:

I've a tabbed activity, I want to show in the first fragment (Tab1History) a Recyclerview and Cardview. I've created a XML file for CardView, and in the fragment XML I've insert the Recyclerview. I've also created the adapter:

public class CespiteAdapter extends RecyclerView.Adapter<CespiteAdapter.ViewHolder>
{
    private List<CespiteOgg> cespiteOggList;
    private Context context;

public CespiteAdapter(List<CespiteOgg> cespiteOggList, Context context) {
    this.cespiteOggList = cespiteOggList;
    this.context = context;
}

public class ViewHolder extends RecyclerView.ViewHolder
{
    public CardView cv;
    public TextView txtNumInventario;
    public TextView txtNomeCespite;
    public TextView txtDtCatalogazione;
    public TextView txtAula;
    public TextView txtNomeUser;


    ViewHolder(View itemView)
    {
        super (itemView);
        //cv = (CardView) itemView.findViewById(R.id.cardView);
        txtNumInventario = (TextView) itemView.findViewById(R.id.txtNumeroInventario);
        txtNomeCespite = (TextView) itemView.findViewById(R.id.txtNomeCespite);
        txtDtCatalogazione = (TextView) itemView.findViewById(R.id.txtDataCatalogazione);
        txtAula = (TextView) itemView.findViewById(R.id.txtAula);
        txtNomeUser= (TextView) itemView.findViewById(R.id.txtNomeUser);

    }
}



@Override
public CespiteAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    View cespiteView = inflater.inflate(R.layout.cespite_card_view, parent, false);

    return new ViewHolder(cespiteView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
    CespiteOgg cespiteOgg = cespiteOggList.get(position);

    holder.txtNumInventario.setText(cespiteOgg.getNumInventario());
    holder.txtNomeCespite.setText(cespiteOgg.getNomeCespite());
    holder.txtDtCatalogazione.setText(cespiteOgg.getDtCatalogazione().toString());
    holder.txtAula.setText(cespiteOgg.getAula());
    holder.txtNomeUser.setText(cespiteOgg.getNomeUser());

}

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

这是片段:

 public class Tab1History extends Fragment
{



 private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;

private List<CespiteOgg> cespiteOggList;
private Date location;
Date date = new Date(location.getTime());



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.cespite_card_view, container, false);

    recyclerView = (RecyclerView) getView().findViewById(R.id.RecyclerView);
    recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    cespiteOggList = new ArrayList<>();

    for(int i=0; i<=10; i++)
    {
        CespiteOgg cespiteOgg = new CespiteOgg("heading"+(i+1), "Lorem ipsum", date, "sdjijsad", "jdkjd");

        cespiteOggList.add(cespiteOgg);

    }

    adapter = new CespiteAdapter(cespiteOggList, getContext());

    recyclerView.setAdapter(adapter);

            return rootView;
}
}

这是堆栈跟踪:

FATAL EXCEPTION: main
                                                                       Process: com.example.arslan.qrcode, PID: 14311
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference

这是cespite_card_view XML:

Here's cespite_card_view XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardView">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imgCespite"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="16dp"
        android:src="@mipmap/thebigger" />

    <TextView
        android:id="@+id/txtNumeroInventario"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/imgCespite"
        android:text="text1" />

    <TextView
        android:id="@+id/txtNomeCespite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtNumeroInventario"
        android:layout_toRightOf="@id/imgCespite"
        android:text="text2" />

    <TextView
        android:id="@+id/txtDataCatalogazione"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtNomeCespite"
        android:layout_toRightOf="@id/imgCespite"
        android:text="text3" />


    <TextView
        android:id="@+id/txtAula"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtDataCatalogazione"
        android:layout_toRightOf="@id/imgCespite"
        android:text="text4" />

    <TextView
        android:id="@+id/txtNomeUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtAula"
        android:layout_toRightOf="@id/imgCespite"
        android:text="text5" />

</RelativeLayout>

这是片段布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.arslan.qrcode.Main"
android:layout_weight="1"
>

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/my_recycler_view"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

推荐答案

更改此行:

recyclerView = (RecyclerView) getView().findViewById(R.id.RecyclerView);

对此:

recyclerView = (RecyclerView) rootView.findViewById(R.id.RecyclerView);

另外,您在此行中的 RecyclerView.Adapter Fragment 使用相同的布局:

Also, you are using the same layout for your RecyclerView.Adapter and your Fragment in this line:

 View rootView = inflater.inflate(R.layout.cespite_card_view, container, false);

您应该为片段设置一个布局文件,其中包含您的 RecyclerView 小部件,如下所示:

You should have a layout file for your fragment that contains your RecyclerView widget inside it, like this:

<android.support.v7.widget.RecyclerView
 android:id="@+id/recyclerView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#e9e9e9 />

这篇关于&amp; quot; RecyclerView.setHasFixedSize(boolean)&amp;#39;在空对象引用上".Recyclerview变成片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆