2014-03-26 47 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

相關問題