我想显示一个自定义对话框,其中有一个列表视图。首先看看我的代码如下。NullpointerException在对话框中为列表视图设置适配器
对话框:
protected void onPostExecute(String file_url) {
btnInvite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater inflater = getActivity().getLayoutInflater();
Dialog dialog = new Dialog(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
ListView lv = (ListView) dialog.findViewById(R.id.lvAddDialog);
ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
lv.setAdapter(adapter);
builder.setView(inflater.inflate(R.layout.dialog_add, null))
.setTitle("Invite people")
.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
dialog = builder.create();
dialog.show();
}
});
}
}
适配器:
public class ListviewContactAdapter extends BaseAdapter{
private static ArrayList<ListviewContactItem> listContact;
private LayoutInflater mInflater;
public ListviewContactAdapter(Context photosFragment, ArrayList<ListviewContactItem> results){
listContact = results;
mInflater = LayoutInflater.from(photosFragment);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listContact.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return listContact.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.contact_item, null);
holder = new ViewHolder();
holder.txtname = (TextView) convertView.findViewById(R.id.lv_contact_item_name);
holder.txtphone = (TextView) convertView.findViewById(R.id.lv_contact_item_phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtname.setText(listContact.get(position).GetName());
holder.txtphone.setText(listContact.get(position).GetPhone());
return convertView;
}
static class ViewHolder{
TextView txtname, txtphone;
}
}
当我运行这个应用程序显示的是在的NullPointerException错误:
ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
我GOOGLE了它,但仍不能找到哪里错了。寻求帮助。
我不明白你为什么希望找到'lvAddDialog'对话框 – Blackbelt
内部可能listcontact为空? –
这只是一个名字@blackbelt – gamo