Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListPopupWindow getListView在创建后返回nullpointerexception_Java_Android - Fatal编程技术网

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()的位置