尝试在null对象引用onCreateView(Fragment.java)上调用虚拟方法RecyclerView

时间:2019-04-12 03:50:50

标签: java android android-fragments

我试图在Fragment中将数据显示到RecyclerView中,但出现错误:

Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
        at hsp.net.id.absensikendaraan.activity.FrgHistoryUsers.onCreateView(FrgHistoryUsers.java:76)

我已经检查过findViewById(R.id.rv_view)是否引用了正确的布局,并且还将其放置在onCreateView和数据调用方法getData()下。

由于出现该错误,我仍然困惑应该将代码放在onCreate还是onCreateView中。

这是我的片段代码:

public class FrgHistoryUsers extends Fragment {

    RecyclerView recyclerView;
    AdapterHistoryUser adapterHistoryUser;
    List<RentItem> rentItems;

    public FrgHistoryUsers() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        recyclerView = container.findViewById(R.id.rv_listhistory);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new MyDividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL, 16));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);

        getDataRent();

        return view;
    }

    private void getDataRent() {
        RestApi api = RetroFit.getInstanceRetrofit();
        Call<Rent> getData = api.getDataRent();
        getData.enqueue(new Callback<Rent>() {
            @Override
            public void onResponse(Call<Rent> call, Response<Rent> response) {

                rentItems = new ArrayList<RentItem>();
                rentItems = response.body().getRent();
                final String[] itmImage = new String[rentItems.size()];
                final String[] itmDestiny = new String[rentItems.size()];
                final String[] itmName = new String[rentItems.size()];
                final String[] itmBrand = new String[rentItems.size()];
                final String[] itmPlate = new String[rentItems.size()];
                final String[] itmStatus = new String[rentItems.size()];
                final String[] itmDepart = new String[rentItems.size()];
                final String[] itmReturn = new String[rentItems.size()];
                for (int i = 0; i < rentItems.size(); i++) {
                    //Storing names to string array
                    itmImage[i] = rentItems.get(i).getDPict();
                    itmDestiny[i] = rentItems.get(i).getDDestination();
                    itmName[i] = rentItems.get(i).getDNameuser();
                    itmBrand[i] = rentItems.get(i).getDBrand();
                    itmPlate[i] = rentItems.get(i).getDPlatenumber();
                    itmStatus[i] = rentItems.get(i).getDApproval();
                    itmDepart[i] = rentItems.get(i).getDDeparture();
                    itmReturn[i] = rentItems.get(i).getDArrival();
                }

                adapterHistoryUser = new AdapterHistoryUser(getContext(),rentItems);
                recyclerView.setAdapter(adapterHistoryUser);

            }

            @Override
            public void onFailure(Call<Rent> call, Throwable t) {
                Toast.makeText(getContext(), getString(R.string.error_msg), Toast.LENGTH_LONG).show();
            }
        });
    }
}

适配器文件:

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

    private Context context;
    private final List<RentItem> listRent;

    public AdapterHistoryUser(Context context, List<RentItem> items) {
        this.context = context;
        this.listRent = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.users_item_history, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Glide.with(context)
                .load(listRent.get(position).getDPict())
                .placeholder(R.drawable.ic_placeholder)
                .error(R.drawable.ic_placeholder)
                .into(holder.iv_pict);

        holder.tvDestiny.setText(listRent.get(position).getDDestination());
        holder.tvRenter.setText(listRent.get(position).getDNameuser());
        holder.tvBrand.setText(listRent.get(position).getDBrand());
        holder.tvPlate.setText(listRent.get(position).getDPlatenumber());
        holder.tvDepart.setText(listRent.get(position).getDDeparture());
        holder.tvReturn.setText(listRent.get(position).getDArrival());
        switch (listRent.get(position).getDApproval()) {
            case "0":
                holder.tvStatus.setText(R.string.txt_hstatus);
                holder.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.hsp_red));
                break;
            case "1":
                holder.tvStatus.setText(R.string.txt_hstatus2);
                holder.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.hsp_yellow));
                break;
            case "2":
                holder.tvStatus.setText(R.string.txt_hstatus3);
                holder.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.green));
                break;
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_pict;
        TextView tvDestiny, tvRenter, tvBrand, tvPlate, tvDepart, tvReturn, tvStatus;

        public ViewHolder(View view) {
            super(view);
            iv_pict = view.findViewById(R.id.h_pict);
            tvDestiny = view.findViewById(R.id.h_destiny);
            tvRenter = view.findViewById(R.id.h_renter);
            tvBrand = view.findViewById(R.id.h_brand);
            tvPlate = view.findViewById(R.id.h_plate);
            tvDepart = view.findViewById(R.id.h_depart);
            tvReturn = view.findViewById(R.id.h_return);
            tvStatus = view.findViewById(R.id.h_status);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/rv_listhistory"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:scrollbars="vertical"
    tools:context=".activity.FrgHistoryUsers"
    tools:listitem="@layout/users_item_history" />

预先感谢

0 个答案:

没有答案