我正在使用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?

10-01 07:50