1

I'm using the 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;
}

And when I try to call:

listPopupWindow.setAdapter(new DropDownlistAdapter(mContext,
                armAwayItems));
View container = (View) listPopupWindow.getListView().getParent();

It always returns NPE, but this method already exists in the ListPopupWindow:

 Attempt to invoke virtual method 'android.view.ViewParent android.widget.ListView.getParent()' on a null object reference

The same when I try to call

listPopupWindow.getListView().setDividerHeight(1);

and

listPopupWindow.getListView().setDivider(...);

So listPopupWindow.getListView() always returns NULL.

Rikki Tikki Tavi
  • 3,089
  • 5
  • 43
  • 81

2 Answers2

0

try this

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?

Community
  • 1
  • 1
Jaskaran Singh
  • 127
  • 5
  • 12
0

For those who look for answer in answer section (however M D's comment was much much earlier):

getListView() will return null until you call show() method of the listPopupWindow

skv
  • 85
  • 9