设置适配器时NullPointerException异常的列表视图对话框适配器、视图、对话框、异常

2023-09-03 23:10:18 作者:沉默

我要显示自定义对话框,在它里面一个列表视图。首先对我下面的code一起来看看。

I want to display a custom dialog that have a listview inside it. First take a look on my code below.

对话:

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异常为:

When I run the app show up an error that NullpointerException at:

ListviewContactAdapter adapter = new ListviewContactAdapter(getActivity(), listContact);

我GOOGLE了它,但仍然无法找到哪里错了。寻求帮助。

I googled it but still can't find where wrong. Looking for help.

推荐答案

充气视图,并使用充气返回的对象来寻找的ListView 里面的布置

Inflate your view and use the object returned by the inflater to look for the ListView inside the layout

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