ListPopupWindow:getWindowVisibleDisplayFrame(Rect) 空异常

ListPopupWindow: getWindowVisibleDisplayFrame(Rect) null exception

当我单击工具栏中的项目时,我想展开一个列表弹出窗口,但它不起作用。

onOptionsItemSelected(MenuItem item) 方法内部

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();

// ...

异常

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.onMenuItemClick(ToolbarActionBar.java:72)
        at android.support.v7.widget.Toolbar.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)

问题是您的锚点视图无效。 我遇到了同样的问题并用这个解决方案修复了它:

popup.setAnchorView(getActivity().findViewById(R.id.idOfYourMenuItem));

当使用显示 drop-down 列表的 AutoCompleteTextView 并且 AutoCompleteTextView 包含 android:dropDownAnchor 引用的外部资源 ID 时会出现此问题。

出于某种原因,它只在使用 buildToolsVersion '26.0.2' 时发生。这是一个例子:

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>

在 Android 6 月 7 日,它像这样崩溃(当试图显示下拉列表时):

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

在 Android 8 上,它像这样崩溃:

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

为避免崩溃,您应该将外部 ID 转换为常规内部 ID,或使用 Java 代码:textField.setDropDownAnchor(...);

旧的构建工具版本没有这个问题。

是的,问题是因为传递给 setAnchorView 的视图无效。

要轻松解决此问题,请将视图从点击事件处理程序传递给 setAnchorView

例如

public void handlerName(View view) {
        ListPopupWindow popup = new ListPopupWindow(getBaseContext());
        popup.setAdapter(new ArrayAdapter<>(getBaseContext(), R.layout.dialog_checkview, 
        R.id.checkview_title, stringList));
        popup.setAnchorView(view);
        popup.setModal(true);
        popup.show();
}