" RecyclerView.setHasFixedSize(布尔值)'在null对象引用" Recyclerview into fragment

时间:2017-06-08 12:02:44

标签: android android-fragments android-recyclerview android-cardview

我是一个标签式活动,我想在第一个片段(Tab1History)中显示Recyclerview和Cardview。我为CardView创建了一个XML文件,在片段XML中我插入了Recyclerview。我还创建了适配器:

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:

<?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>

3 个答案:

答案 0 :(得分:3)

更改此行:

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

对此:

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

此外,您对此行中的RecyclerView.AdapterFragment使用了相同的布局:

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

您的片段应该有一个包含RecyclerView小部件的布局文件,如下所示:

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

答案 1 :(得分:0)

getView()更改为rootView

getView - 获取片段布局的根视图(onCreateView(LayoutInflater,ViewGroup,Bundle)返回的布局),如果提供的话。

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

    recyclerView = (RecyclerView) rootView.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;
}

答案 2 :(得分:0)

您应该从充气视图中找到您的视图。 使用:

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

而不是:

recyclerView = (RecyclerView) getView().findViewById(R.id.RecyclerView);
祝你好运。