单击操作栏上的图标到弹出菜单

时间:2017-03-02 19:31:49

标签: java android android-actionbar popupmenu

我正在尝试单击操作栏上的图标以显示弹出菜单。问题是,当我单击操作栏上的图标时,应用程序会强制关闭,弹出菜单不会显示。我的代码出了什么问题?谢谢你的帮助。

我收到此错误:

java.lang.RuntimeException:无法解析索引6处的属性

请参阅下面的Logcat完整输出。

代码:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_action, menu);

        return true;
    }


@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_sort:

              View menuItemView = findViewById(R.id.action_sort);
              PopupMenu popupMenu = new PopupMenu(this, menuItemView);
              popupMenu.inflate(R.menu.popup_menu);
              popupMenu.show();

              return true;
     }

    return super.onOptionsItemSelected(item);
}

menu_action.xml

<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>

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/opt1"
        android:title="Sort" />
    <item
        android:id="@+id/opt2"
        android:title="A to Z" />

    <item
        android:id="@+id/opt3"
        android:title="Z to A" />
</menu>

logcat的:

03-02 09:43:53.711  13666-13666/com.test.audio E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test.audio, PID: 13666
    java.lang.RuntimeException: Failed to resolve attribute at index 6
            at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603)
            at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6473)
            at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6641)
            at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:741)
            at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:685)
            at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:62)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:370)
            at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219)
            at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153)
            at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:125)
            at android.support.v7.widget.PopupMenu.show(PopupMenu.java:193)
            at com.test.audio.TouchActivity.onOptionsItemSelected(TouchActivity.java:2417)
            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)

0 个答案:

没有答案