我正在尝试显示一个Dialog片段,其中包含所有联系人列表。
我已经制作了这个适配器:
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)
我该如何解决这个问题?是否存在执行周期问题(试图给不存在的元素充气)
尝试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)
答案 0 :(得分:1)
尝试像这样在适配器的getView中对vi View进行充气。
if(vi==null){
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi=inflater.inflate(R.layout.item_contact,parent,false);
}