我正在使用ListPopupWindow
:
@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
,但是ListPopupWindow
中已经存在此方法: Attempt to invoke virtual method 'android.view.ViewParent android.widget.ListView.getParent()' on a null object reference
我打电话的时候也一样
listPopupWindow.getListView().setDividerHeight(1);
和
listPopupWindow.getListView().setDivider(...);
因此
listPopupWindow.getListView()
始终返回NULL。 最佳答案
尝试这个
if(listPopupWindow != null){
listPopupWindow.setAdapter(new DropDownlistAdapter(mContext,
armAwayItems));
View container = (View) listPopupWindow.getListView().getParent();
What is a Null Pointer Exception, and how do I fix it?