Java ListPopupWindow getListView在创建后返回nullpointerexception
我正在使用Java ListPopupWindow getListView在创建后返回nullpointerexception,java,android,Java,Android,我正在使用列表PopupWindow: @NonNull private ListPopupWindow createListPopupWindow() { ListPopupWindow listPopupWindow = new ListPopupWindow(mContext); listPopupWindow.setBackgroundDrawable(mContext.getDrawable(R.drawable.custom_dropdown)); listP
列表PopupWindow
:
@NonNull
private ListPopupWindow createListPopupWindow() {
ListPopupWindow listPopupWindow = new ListPopupWindow(mContext);
listPopupWindow.setBackgroundDrawable(mContext.getDrawable(R.drawable.custom_dropdown));
listPopupWindow.setDropDownGravity(Gravity.CENTER);
listPopupWindow.setWidth(ListPopupWindow.WRAP_CONTENT);
listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
listPopupWindow.setModal(true);
return listPopupWindow;
}
当我试着打电话时:
listPopupWindow.setAdapter(new DropDownlistAdapter(mContext,
armAwayItems));
View container = (View) listPopupWindow.getListView().getParent();
它总是返回NPE
,但此方法已存在于列表PopupWindow
中:
Attempt to invoke virtual method 'android.view.ViewParent android.widget.ListView.getParent()' on a null object reference
我打电话的时候也是一样
listPopupWindow.getListView().setDividerHeight(1);
及
因此listPopupWindow.getListView()
总是返回NULL 试试这个
if(listPopupWindow != null){
listPopupWindow.setAdapter(new DropDownlistAdapter(mContext,
armAwayItems));
View container = (View) listPopupWindow.getListView().getParent();
试试这个
if(listPopupWindow != null){
listPopupWindow.setAdapter(new DropDownlistAdapter(mContext,
armAwayItems));
View container = (View) listPopupWindow.getListView().getParent();
对于那些在答案部分寻找答案的人(不过MD的评论要早得多):
getListView()将返回null,直到您调用listPopupWindow的show()方法(对于那个些在answer部分中查找答案的人)(不过MD的评论要早得多): getListView()将返回null,直到调用listPopupWindow的show()方法,您在其中调用了
show()代码>?调用show()的位置代码>?