android中的對話框,布局文件:
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
在Activity中給listView添加一個Adapter:
@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case ADDPLAYERDIALOG:{
Dialog d = new Dialog(this);
d.setContentView(R.layout.training_dialog);
ListView lv = (ListView) d.getCurrentFocus().getRootView();
ListAdapter adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, createNamesList());
lv.setAdapter(adapter);
return d;
}
}
return super.onCreateDialog(id);
}
結果出現NullPointerException:
ListView lv = (ListView) d.getCurrentFocus().getRootView();
沒有這個ListView小部件的ID,因為是XML布局文件,不能寫lv = d.findViewById(R.id.listview);
請高手指教。
Dialog d = new Dialog(this);
ListView lv = (ListView) View.inflate(getBaseContext(),R.layout.training_dialog,null);
d.setContentView(lv);
ListAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, createNamesList());
lv.setAdapter(adapter);