CardView with Fragment is it possible?

1

I'm trying to show a CardviewLayout inside a Fragment, but I can not do it, it shows the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                   at ibk.center.contact.demo.democontactcenteribk.adapter.ContactAdapter.onBindViewHolder(ContactAdapter.java:33)
                                                   at ibk.center.contact.demo.democontactcenteribk.adapter.ContactAdapter.onBindViewHolder(ContactAdapter.java:17)

My ContactAdapter Class:

    private List<ContactInfo> contactList;

public ContactAdapter(List<ContactInfo> contactList) {
    this.contactList = contactList;
}

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

@Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
    ContactInfo ci = contactList.get(i);
    contactViewHolder.vName.setText(ci.name);
    contactViewHolder.vSurname.setText(ci.surname);
    contactViewHolder.vEmail.setText(ci.email);
    //contactViewHolder.vTitle.setText(ci.name + " " + ci.surname);
    contactViewHolder.vTitle.setText(ci.fechaEntrada);
    contactViewHolder.vSede.setText(ci.sede);
}

@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.activity_cardview, viewGroup, false);

    return new ContactViewHolder(itemView);
}

public static class ContactViewHolder extends RecyclerView.ViewHolder {

    TextView vName;
    TextView vSurname;
    TextView vEmail;
    TextView vTitle;
    TextView vSede;

    public ContactViewHolder(View v) {
        super(v);
        vName = (TextView) v.findViewById(R.id.txtName);
        vSurname = (TextView) v.findViewById(R.id.txtSurname);
        vEmail = (TextView) v.findViewById(R.id.txtEmail);
        vTitle = (TextView) v.findViewById(R.id.title);
        vSede = (TextView) v.findViewById(R.id.txtAdd);
    }
}

And my Fragment:

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    RecyclerView recList = (RecyclerView) getActivity().findViewById(R.id.cardList);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());

    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    ContactAdapter ca = new ContactAdapter(createList(30));
    recList.setAdapter(ca);
}

That code is in the Fragment, but I have not modified the MainActivity.

The side menu if I can show it and the fragment without the CardView too, but when I put the code is what gives me that error, I think you can not find it in the view or something out there or the controls

I mark error in this line:

ci.name = sdfTiempo.format(unaFecha.getTime()).toString();

I hope you can help me, Thanks

    
asked by Kenny 19.11.2016 в 23:20
source

2 answers

0

My mistake, I put the code in the wrong place, it was not in:

onViewCreated

if not in:

onCreateView

Thanks

    
answered by 20.11.2016 в 14:13
0

Of course it's possible, as for your error:

  

java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.TextView.setText (java.lang.CharSequence) 'on a null   object reference                                                      at ibk.center.contact.demo.democontactcenteribk.adapter.ContactAdapter.onBindViewHolder (ContactAdapter.java:33)                                                      at ibk.center.contact.demo.democontactcenteribk.adapter.ContactAdapter.onBindViewHolder (ContactAdapter.java:17)

It is caused because the indicated TextView and the other elements in the container are not found:

vName = (TextView) v.findViewById(R.id.txtName);
        vSurname = (TextView) v.findViewById(R.id.txtSurname);
        vEmail = (TextView) v.findViewById(R.id.txtEmail);
        vTitle = (TextView) v.findViewById(R.id.title);
        vSede = (TextView) v.findViewById(R.id.txtAdd);

The initialization of the containers must be done in onCreateView() .

  

Remember that onCreateView () is the equivalent in the Fragment of    onCreate () for Activities and runs during the creation of the   view. onViewCreated () is executed after the view was created.

    
answered by 20.11.2016 в 16:19