2016-04-30 7 views
1

私はListPopupWindow使用しています:ListPopupWindow getListViewは、作成後にNullPointerExceptionが返されます

@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; 
} 

と私は呼んしようとすると:それは常にNPEを返し

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

を、この方法は、既にListPopupWindowに存在します:

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

Th E同じ私はそうlistPopupWindow.getListView()

listPopupWindow.getListView().setDividerHeight(1); 

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

を呼び出そうと常にNULLを返します。

+1

あなたは 'show();'を呼び出しましたか? –

答えて

0

(ただしMDさんのコメントは、はるか以前あった)解答セクションで答えを探している人のために、この

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?

0

を試してみてください。

getListView()まで、nullを返しますlistPopupWindowのshow()メソッドを呼び出します。

関連する問題