Shixons
Shixons

Reputation: 197

PopupMenu inside Adapter getView() not working

I'm using a custom adapter and adding this code on getView() method:

final ImageView popupMenu = (ImageView) v.findViewById(R.id.popupMenu);
    popupMenu.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            popupMenu.setImageResource(R.drawable.ic_popup_menu_selected);
            PopupMenu popup = new PopupMenu(context, view);
            popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
            popup.setOnDismissListener(new PopupMenu.OnDismissListener() {
                @Override
                public void onDismiss(PopupMenu pm) {
                    popupMenu.setImageResource(R.drawable.ic_popup_menu);
                }
            });
            popup.show();
        }
});

But I get this error: java.lang.RuntimeException: Failed to resolve attribute at index 6

The same code works onListItemClick() but it doesn't make sense to be there because it needs to be clicked once to setup and click again to trigger the PopupMenu.

Edit: Logcat

11-02 17:58:51.276    1996-1996/com.android.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.android.example, PID: 1996
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:6423)
        at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6591)
        at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:735)
        at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:679)
        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:363)
        at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:212)
        at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:146)
        at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:118)
        at android.support.v7.widget.PopupMenu.show(PopupMenu.java:168)
        at com.android.example.GetAdapter$listAdapter$1.onClick(GetAdapter.java:81)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        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:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Upvotes: 2

Views: 2568

Answers (3)

SUPERCILEX
SUPERCILEX

Reputation: 4007

With the following import I always had the error:

import android.support.v7.widget.PopupMenu;

It works fine with the following import:

import android.widget.PopupMenu;

Full answer: stackoverflow.com/a/27826670/4548500

Upvotes: 5

Amir Uval
Amir Uval

Reputation: 14883

This issue is related to missing theme attributes.
Make sure that your app theme extends Theme.AppCompat.

 <style name="AppTheme" parent="@style/Theme.AppCompat" />

Upvotes: 0

Shixons
Shixons

Reputation: 197

I solve it this way:

Add this on adapter getView():

ImageView popupMenu = (ImageView) v.findViewById(R.id.popupMenu); popupMenu.setTag(getItem(position)); popupMenu.setOnClickListener(MyFragment.popupMenuListener);

And this on MyFragment:

implements OnClickListener

popupMenuListener = this;

@Override public void onClick(final View view) { view.post(new Runnable() { @Override public void run() { showPopupMenu(view); } }); }

Upvotes: 0

Related Questions