2014-03-26 45 views
0

我想显示一个自定义对话框,其中有一个列表视图。首先看看我的代码如下。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了它,但仍不能找到哪里错了。寻求帮助。

+0

我不明白你为什么希望找到'lvAddDialog'对话框 – Blackbelt

+0

内部可能listcontact为空? –

+0

这只是一个名字@blackbelt – gamo

回答

0

虚增您的看法,并使用充气到返回的对象在布局内寻找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); 
+0

完美。它运作良好。谢谢。 – gamo

0

更改此:

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

这样:

ListviewContactAdapter adapter = new ListviewContactAdapter(YourCLassName.this, listContact); 

OR,可能是你没有初始化listContact

+0

谢谢,但它没有奏效。而listContact可以很好地与另一个函数结合使用。 – gamo

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

}

+0

我试过了,但没有奏效。 – gamo

相关问题