在自定义对话框片段上布局充气器 return 空指针异常

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

我正在尝试显示一个对话框片段,其中包含所有联系人的列表。

我制作了这个适配器:

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;
    }
}

我制作了这个对话片段:

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();
    }
}

我这样调用对话框:

 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");
            }
        });

但我总是会遇到这个错误:

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)

我该如何解决这个问题?是否存在执行周期的问题(试图inflate一个不存在的元素)

编辑

尝试使用 Malik 方式,我修改了我的适配器以添加上下文,但我遇到了同样的错误:

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)

我也在尝试这个:contactList.setAdapter(new contactListAdapter(getActivity(), liste)); 但它行不通!只需将我的适配器构造函数也修改为 public contactListAdapter(Context context, List<Contact> liste)

尝试像这样在适配器的 getView 中扩充 vi 视图。

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