我有使用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。我搜索但没有。也许我忘了什么,我不知道。谢谢你的帮助
答案 0 :(得分:0)
因为ListView
在Dialog布局中,所以使用Dialog上下文在listView1
之后初始化setContentView
:
ListView listView1 = (ListView)builder.findViewById(R.id.listView1);