我想显示一个自定义对话框,里面有一个listview。首先看看下面的代码。
对话框:
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);
我用谷歌搜索但仍无法找到错误的地方。寻求帮助。
答案 0 :(得分:0)
改变这个:
ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
到此:
ListviewContactAdapter adapter = new ListviewContactAdapter(YourCLassName.this, listContact);
或者,您可能没有初始化listContact
答案 1 :(得分:0)
给视图充气并使用inflater返回的对象在布局中查找ListView
View view = inflater.inflate(R.layout.dialog_add, null)
ListView lv = (ListView) view.findViewById(R.id.lvAddDialog);
ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
lv.setAdapter(adapter);
builder.setView(view);
答案 2 :(得分:0)
当您获得ListView但未在对话框中设置te布局时。您需要首先构建对话框,然后获取ListView
检查一下:
protected void onPostExecute(String file_url) {
btnInvite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater inflater = getActivity().getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
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 dialog = builder.create();
ListView lv = (ListView) dialog.findViewById(R.id.lvAddDialog);
ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
lv.setAdapter(adapter);
dialog.show();
}
});
}
}