Search code examples
androidnullpointerexceptionandroid-adapterandroid-dialogfragmentlayout-inflater

Layout inflater on a custom Dialog fragment return a null pointer exception


I'm trying to show a Dialog fragment with a list of all contacts in there.

I've made this adapter :

package fr.nf.smsplus.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import fr.nf.smsplus.object.Contact;
import fr.nf.smsplus.R;
import fr.nf.smsplus.contactsDialogFragment;

/**
 * Created by Nicolas on 22/05/2016.
 */
public class contactListAdapter extends BaseAdapter {

    private List<Contact> liste;
    private static LayoutInflater inflater = null;

    public contactListAdapter(contactsDialogFragment contactsDialogFragment, List<Contact> liste) {
        this.liste = liste;
        //inflater = (LayoutInflater) contactsDialogFragment.getSystemService(contactsDialogFragment.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return liste.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return liste.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Contact contact = liste.get(position);
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.item_contact, null);
        TextView text = (TextView) vi.findViewById(R.id.contact_name);
        text.setText(contact.getDisplayName());
        return vi;
    }
}

I've made this dialogfragment :

package fr.nf.smsplus;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;

import java.util.List;

import fr.nf.smsplus.adapter.contactListAdapter;
import fr.nf.smsplus.object.Contact;

/**
 * Created by Nicolas on 22/05/2016.
 */
public class contactsDialogFragment extends DialogFragment{

    List<Contact> liste;
    AbsListView contactList;

    public static contactsDialogFragment newInstance(List<Contact> liste){
        contactsDialogFragment retour = new contactsDialogFragment();
        retour.liste = liste;
        return retour;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View contactListView = inflater.inflate(R.layout.dialog_contacts, null);
        contactList = (AbsListView) contactListView.findViewById(R.id.listContacts);
        contactList.setAdapter(new contactListAdapter(this, liste));

        builder.setView(contactListView)
                .setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                })
                .setTitle(R.string.cdf_title);;



        // Create the AlertDialog object and return it
        return builder.create();
    }
}

I called the dialog like this :

 fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                contactList = new ArrayList<Contact>();
                listContacts();

                contactsDialogFragment pickContact = contactsDialogFragment.newInstance(contactList);
                pickContact.show(getFragmentManager(), "contacts");
            }
        });

But i will always have this error :

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference at fr.nf.smsplus.adapter.contactListAdapter.getView(contactListAdapter.java:52)

How can i fix this problem ? Is there a problem of execution cycle (trying to inflate a inexisting element)

EDIT

Trying Malik way, i've modified my adapter to add the context, and i got sameerror :

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at fr.nf.smsplus.adapter.contactListAdapter.getView(contactListAdapter.java:55)

I'm trying too this : contactList.setAdapter(new contactListAdapter(getActivity(), liste)); And it will not work ! Just modifying too my adapter constructor to public contactListAdapter(Context context, List<Contact> liste)


Solution

  • Try to inflate the vi View in getView of your adapter like this.

    if(vi==null){
       inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       vi=inflater.inflate(R.layout.item_contact,parent,false);
    }