当我点击工具栏中的某个项目时,我想展开一个listpopup,但它不起作用。
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$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)
答案 0 :(得分:9)
问题是您的锚点视图无效。 我遇到了同样的问题并用这个解决方案修复了它:
UriComponents.AbsoluteUri
答案 1 :(得分:0)
使用显示下拉列表的AutoCompleteTextView时会发生此问题,并且AutoCompleteTextView包含android:dropDownAnchor
引用的外部资源ID。
出于某种原因,只有在使用buildToolsVersion '26.0.2'
时才会发生这种情况。这是一个例子:
<强> RES /布局/ my_layout.xml:强>
<AutoCompleteTextView
android:id="@+id/text_field"
android:dropDownAnchor="@id/external_id"
...
/>
<强> RES /值/ 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(...);
较旧的构建工具版本没有此问题。
答案 2 :(得分:0)
是的,问题是因为传递给setAnchorView
的视图无效。
要轻松解决此问题,请将视图从click事件处理程序传递到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();
}