Android在findViewById时返回空ListView
我创建了一个自定义对话框(带有列表视图):Android在findViewById时返回空ListView,android,android-layout,android-listview,Android,Android Layout,Android Listview,我创建了一个自定义对话框(带有列表视图): final ArrayList pokemons=databaseAccess.getAllPokemonWithTypes(type1,type2); //加载自定义的_dialog.xml布局并充气查看 LayoutInflater LayoutInflater=getLayoutInflater(); pokemonFoundListView=(ListView)findviewbyd(R.id.pokemon\u list\u视图); pok
final ArrayList pokemons=databaseAccess.getAllPokemonWithTypes(type1,type2);
//加载自定义的_dialog.xml布局并充气查看
LayoutInflater LayoutInflater=getLayoutInflater();
pokemonFoundListView=(ListView)findviewbyd(R.id.pokemon\u list\u视图);
pokemonAdapter=新的pokemonAdapter(这个,pokemons);
setAdapter(pokemonAdapter);
View customizedUserView=(View)LayoutFlater.inflate(R.layout.customized_对话框,空);
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(上下文);
alertDialogBuilder.setView(customizedUserView);
AlertDialog AlertDialog=alertDialogBuilder.create();
setAdapter(pokemonAdapter,新的DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,int项){
}
});
alertDialog.show();
但是,如果我把这个列表视图放在我的content_main.xml(MainActivty视图)中,Android会找到它。我想这里的问题是,我不能从另一个XML视图中引用另一个元素,该视图不是我的content\u main.XML
如何修复此问题?尝试在您的custome视图上调用findViewById
...
View customizedUserView = (View) layoutinflater.inflate(R.layout.customized_dialog, null);
pokemonFoundListView = (ListView) customizedUserView.findViewById(R.id.pokemon_list_view);
...
希望对您有所帮助您的对话框以惰性方式添加到视图层次结构中 因此,如果尝试从活动根视图中查找listview(在对话框视图中),则将找不到它。(想想ViewStub) 但是视图在对话框中,因此
View customizedUserView = (View) layoutinflater.inflate(R.layout.customized_dialog, null);
pokemonFoundListView = (ListView) customizedUserView.findViewById(R.id.pokemon_list_view);
将正确找到视图
final ArrayList<Pokemon> pokemons = databaseAccess.getAllPokemonWithTypes(type1, type2);
// load the customized_dialog.xml layout and inflate to view
LayoutInflater layoutinflater = getLayoutInflater();
pokemonFoundListView = (ListView) findViewById(R.id.pokemon_list_view);
pokemonAdapter = new PokemonAdapter(this, pokemons);
pokemonFoundListView.setAdapter(pokemonAdapter);
View customizedUserView = (View) layoutinflater.inflate(R.layout.customized_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(customizedUserView);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialogBuilder.setAdapter(pokemonAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
alertDialog.show();
...
View customizedUserView = (View) layoutinflater.inflate(R.layout.customized_dialog, null);
pokemonFoundListView = (ListView) customizedUserView.findViewById(R.id.pokemon_list_view);
...
View customizedUserView = (View) layoutinflater.inflate(R.layout.customized_dialog, null);
pokemonFoundListView = (ListView) customizedUserView.findViewById(R.id.pokemon_list_view);