我试图在点击操作栏图标时显示弹出菜单。 问题是我强行关闭应用程序并且弹出菜单不显示。我的代码出了什么问题?谢谢你的帮助。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".FileManagerActivity">
<item android:id="@+id/action_sort" android:title="@string/action_sort" android:icon="@drawable/ic_sort"
android:showAsAction="always" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
app:showAsAction="never" />
<item android:id="@+id/action_help" android:title="@string/action_help"
app:showAsAction="never" />
</menu>
menu_action.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/opt1"
android:icon="@drawable/ic_sort"
android:title="Change Picture" />
<item
android:id="@+id/opt2"
android:icon="@drawable/ic_fast_forward"
android:title="Change Password" />
<item
android:id="@+id/opt3"
android:icon="@drawable/ic_fast_rewind"
android:title="Sign Out" />
</menu>
popup_menu.xml
02-28 13:04:45.353 18838-18838/com.test.audio E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.test.audio, PID: 18838
java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor
at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:126)
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:193)
at com.test.audio.TouchActivity.showPopup(TouchActivity.java:5340)
at com.test.audio.TouchActivity.onOptionsItemSelected(TouchActivity.java:5270)
at android.app.Activity.onMenuItemSelected(Activity.java:2885)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1133)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at android.widget.ActionMenuView.invokeItem(ActionMenuView.java:587)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:141)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
logcat的:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'default databasename'),
'username' => env('DB_USERNAME', 'default dbuser'),
'password' => env('DB_PASSWORD', 'default cououoe3098!'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
答案 0 :(得分:1)
不要将MenuItem作为View传递,因为它不是。 所以最好把它像这样的视图
View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.counters_overflow);
// And define click listener here.
popupMenu.show();