gpt4 book ai didi

java - ListPopupWindow getListView 创建后返回 nullpointerException

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:38 28 4
gpt4 key购买 nike

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

关于java - ListPopupWindow getListView 创建后返回 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36953639/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com