Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListPopupWindow: getWindowVisibleDisplayFrame(Rect) null exception

When I click an item in my Toolbar I want to expand a listpopup but it does not work.

Inside the onOptionsItemSelected(MenuItem item) method

ListPopupWindow popup = new ListPopupWindow(getBaseContext());
popup.setAdapter(new ArrayAdapter<>(getBaseContext(), R.layout.dialog_checkview, R.id.checkview_title, stringList));
popup.setAnchorView(item.getActionView());
popup.setModal(true);
popup.show();

// ...

Exception

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.getWindowVisibleDisplayFrame(android.graphics.Rect)' on a null object reference
        at android.widget.PopupWindow.getMaxAvailableHeight(PopupWindow.java:1368)
        at android.widget.PopupWindow.getMaxAvailableHeight(PopupWindow.java:1346)
        at android.support.v7.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1174)
        at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:584)
        at com.mysecretpackage.android.activities.MainActivity.onOptionsItemSelected(MainActivity.java:271)
        at android.app.Activity.onMenuItemSelected(Activity.java:2895)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
        at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
        at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
        at android.support.v7.internal.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:72)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:164)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:740)
        at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
        at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:598)
        at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
        at android.view.View.performClick(View.java:4785)
        at android.view.View$PerformClick.run(View.java:19884)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
like image 689
Jonas Borggren Avatar asked Aug 18 '15 15:08

Jonas Borggren


2 Answers

The problem is that your anchor view is not valid. I had the same problem and fixed it with this solution:

popup.setAnchorView(getActivity().findViewById(R.id.idOfYourMenuItem));
like image 68
lilienberg Avatar answered Oct 26 '22 17:10

lilienberg


This problem happens when using an AutoCompleteTextView that displays a drop-down list, and the AutoCompleteTextView contains an external resource ID for the android:dropDownAnchor reference.

For some reason, it only happens when using buildToolsVersion '26.0.2'. Here's an example:

res/layout/my_layout.xml:

<AutoCompleteTextView
    android:id="@+id/text_field"
    android:dropDownAnchor="@id/external_id"
    ...
/>

res/values/dimens.xml:

<resources>
    <!-- https://developer.android.com/guide/topics/resources/more-resources.html#Id -->
    <item type="id" name="external_id">@+id/text_field</item>
</resources>

On Android 6/7, it crashes like this (when trying to display the drop down list):

java.lang.NullPointerException: Attempt to invoke virtual method
  'void android.view.View.getWindowVisibleDisplayFrame(android.graphics.Rect)'
  on a null object reference

On Android 8, it crashes like this:

java.lang.NullPointerException: Attempt to invoke virtual method 
  'android.os.IBinder android.view.View.getApplicationWindowToken()'
  on a null object reference

To avoid the crash, you should convert the external ID to a regular internal one, or use Java code: textField.setDropDownAnchor(...);

Older build tools versions did not have this problem.

like image 36
Mr-IDE Avatar answered Oct 26 '22 18:10

Mr-IDE