setAdapter()中的NPE

时间:2014-02-08 15:03:25

标签: android android-listview android-adapter

我有使用listview显示对话框的方法。这是我的代码: 方法:

private void showWindow(){
Bitmap [] img = {dz, rz, s, ww};
Dialog builder = new Dialog(myContext);
builder.setTitle("to jest list view");
CustomList adapter = new CustomList(myContext, 
     android.R.layout.simple_list_item_1, img);
builder.setContentView(R.layout.wybierz_kolor);
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter(adapter);
builder.show();
listView1.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    switch(position){
    case 0:
        break;
    case 1:
        break;
    case 2:
        break;
    case 3:
        break;
    }
}
});}

但在行“listView1.setAdapter(adapter);”我有NPE。我搜索但没有。也许我忘了什么,我不知道。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

因为ListView在Dialog布局中,所以使用Dialog上下文在listView1之后初始化setContentView

ListView listView1 = (ListView)builder.findViewById(R.id.listView1);