show();

时间:2016-04-12 18:25:13

标签: java android xml

所以我试图创建一个弹出菜单,当用户按下ImageButton时会弹出该菜单。

我在popupMenu.show()上遇到了奇怪的强制关闭错误:

case R.id.action_menu_edit_biljeske_uredi:
  linearLayoutDodajBiljeskuBojeIOstalaSranja.setVisibility(View.VISIBLE);
  imageButtonBiljeskeDodajBiljeskuBojaPozadine
    = (ImageButton) findViewById(R.id.imageButtonBiljeskeDodajBiljeskuBojaPozadine);
  imageButtonBiljeskeDodajBiljeskuBojaPozadine.setOnClickListener(
    new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        PopupMenu popupMenu = new PopupMenu(getApplicationContext(),
                                            imageButtonBiljeskeDodajBiljeskuBojaPozadine);
        popupMenu.getMenuInflater().inflate(R.menu.menu_popup_biljeske_boje,
                                            popupMenu.getMenu());
        popupMenu.show();
      }
    });
}

有谁知道为什么?

logcat的

04-12 20:22:27.847 24594-24594/com.daroioradecic.studyandexamplannerbydario E/AndroidRuntime:
  FATAL EXCEPTION: main
  Process: com.daroioradecic.studyandexamplannerbydario, PID: 24594
  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:6474)
      at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6642)
      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.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:374)
      at android.support.v7.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:223)
      at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:157)
      at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:129)
      at android.support.v7.widget.PopupMenu.show(PopupMenu.java:216)
      at com.daroioradecic.studyandexamplannerbydario.BiljeskeDodajBiljesku$2.onClick(BiljeskeDodajBiljesku.java:106)
      at android.view.View.performClick(View.java:4856)
      at android.view.View$PerformClick.run(View.java:19956)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:211)
      at android.app.ActivityThread.main(ActivityThread.java:5389)
      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:1020)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:icon="@drawable/ic_action_crvena"
        android:title=""
        android:id="@+id/popupMenuBiljeskeBojeCrvena"/>
    <item
        android:icon="@drawable/ic_action_ljubicasta"
        android:title=""
        android:id="@+id/popupMenuBiljeskeBojeLjubicasta"/>
    <item
        android:icon="@drawable/ic_action_svijetloplava"
        android:title=""
        android:id="@+id/popupMenuBiljeskeBojeSvijetloPlava"/>
    <item
        android:icon="@drawable/ic_action_tamnoplava"
        android:title=""
        android:id="@+id/popupMenuBiljeskeBojeTamnoPlava"/>
    <item
        android:icon="@drawable/ic_action_zelena"
        android:title=""
        android:id="@+id/popupMenuBiljeskeBojeZelena"/>
    <item
        android:icon="@drawable/ic_action_usranozuta"
        android:title=""
        android:id="@+id/popupMenuBiljeskeBojeUsranoZuta"/>
    <item
        android:icon="@drawable/ic_action_narancasta"
        android:title=""
        android:id="@+id/popupMenuBiljeskeBojeNarancasta"/>
    <item
        android:icon="@drawable/ic_action_siva"
        android:title=""
        android:id="@+id/popupMenuBiljeskeBojeSiva"/>
</menu>

1 个答案:

答案 0 :(得分:6)

PopupMenu popupMenu = new PopupMenu(getApplicationContext(), imageButtonBiljeskeDodajBiljeskuBojaPozadine);

用YourActivity.this替换getApplicationContext()。我认为你的问题将得到解决。

为什么getApplicationContext()无效,请访问https://possiblemobile.com/2013/06/context/获取详细解决方案。