[英]NullpointerException when setting adapter for listview in dialog
I want to display a custom dialog that have a listview inside it. 我想显示一个自定义对话框,里面有一个列表视图。 First take a look on my code below.
首先看看下面的代码。
Dialog: 对话:
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();
}
});
}
}
Adapter: 适配器:
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;
}
}
When I run the app show up an error that NullpointerException at: 当我运行应用程序时,在以下位置显示NullpointerException错误:
ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
I googled it but still can't find where wrong. 我用谷歌搜索,但仍然找不到错误的地方。 Looking for help.
寻求帮助。
Change this: 更改此:
ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);
to this: 对此:
ListviewContactAdapter adapter = new ListviewContactAdapter(YourCLassName.this, listContact);
OR, may be you didn't initialize listContact
或者,可能是您没有初始化
listContact
Inflate your view and use the object returned by the inflater to look for the ListView
inside the layout 扩展视图并使用充气器返回的对象在布局内查找
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);
When you get the ListView yet not set te layout in the Dialog. 当您获得ListView时,尚未在对话框中设置布局。 You need build first your dialog and after get the ListView
您需要先构建对话框,然后获取ListView
Check this: 检查一下:
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();
}
});
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.