在片段上使用回收器视图时出错

时间:2018-10-06 12:13:18

标签: android android-fragments android-recyclerview nullpointerexception fragment

我正在按片段创建一个Recycler视图,在运行我的应用程序时,应用程序会自动关闭并显示以下错误:

虽然使用片段布局,但显示错误,除了当我使用空布局时,该过程正常运行而且很容易运行。

 Process: com.example.lenovo.skanda, PID: 9018
>     java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.lenovo.skanda.Listitem.getHead()' on a
> null object reference
>         at `enter code here`com.example.lenovo.skanda.MyAdapter.onBindViewHolder(MyAdapter.java:35)
>         at com.example.lenovo.skanda.MyAdapter.onBindViewHolder(MyAdapter.java:12)

需要帮助.. !!

Fragment_stories.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container">

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

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:padding="10dp"
        android:layout_margin="@dimen/cardview_default_elevation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:padding="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textViewHead"
                android:text="Heading"
                android:textSize="15dp"
                android:gravity="center"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textViewDesc"
                android:text="Description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

StoriesFragment.java

public class StoriesFragment extends Fragment {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;

    private List<Listitem> listitems;

    String[] head = new String[]{"apple", "banana", "curry", "dog"};

    String[] desc = new String[]{"This is article 1", "This is article 2", " This is article 3”, "this is article 4"};


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_stories, container, false);


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

        final LinearLayoutManager layoutManager = new  LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);

                listitems = new ArrayList<>();


        for (int i = 0; i <= 3; i++) {
            new Listitem(
                    head[i],
                    desc[i]);
            Listitem listitem = null;

            listitems.add(listitem);

        }

        adapter = new MyAdapter(listitems, getActivity());

        recyclerView.setAdapter(adapter);

        return view;
    }


}

Listitem.java

package com.example.lenovo.skanda;

public class Listitem {

    private String head;
    private String desc;

    public Listitem(String head, String desc) {
        this.head = head;
        this.desc = desc;
    }

    public String getHead() {
        return head;
    }

    public String getDesc() {
        return desc;
    }
}

MyAdapter.java

package com.example.lenovo.skanda;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<Listitem> listitems;
    private Context context;

    public MyAdapter(List<Listitem> listitems, Context context) {
        this.listitems = listitems;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Listitem listitem = listitems.get(position);

        holder.textViewHead.setText(listitem.getHead());
        holder.textViewDesc.setText(listitem.getDesc());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView textViewHead;
        public TextView textViewDesc;

        public ViewHolder(View itemView) {
            super(itemView);

            textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
            textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您的问题与您的周期有关。您正在将可为空的值添加到列表中:

for (int i = 0; i <= 3; i++) {
            new Listitem(
                    head[i],
                    desc[i]);
            Listitem listitem = null;

            listitems.add(listitem);

        }

应该是:

for (int i = 0; i <= 3; i++) {

            Listitem listitem = new Listitem(
                    head[i],
                    desc[i]);

            listitems.add(listitem);

       }