0
votes

So I setup a CardView to open a PopupMenu, but it just keeps crashing, no matter what I have tried.

Here's the XML:

<android.support.v7.widget.CardView
    android:id="@+id/cardview_gender"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/edittext_height"
    android:layout_marginBottom="25dp"
    android:onClick="getGender"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    app:cardBackgroundColor="@color/colorTransparent"
    app:cardElevation="0dp">

            <TextView
                android:id="@+id/edittext_gender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hint_gender"
                android:textColor="@color/colorSecondaryText"
                android:textSize="14sp" />

</android.support.v7.widget.CardView>

And here's the method:

public void getGender(View view) {

    PopupMenu popupMenu = new PopupMenu(this, view);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.item_girl:
                    editText_gender.setText(string_boy);
                    return true;
                case R.id.item_boy:
                    editText_gender.setText(string_girl);
                    return true;
                default:
                    return true;
            }
        }
    });
    popupMenu.inflate(R.menu.gender_menu);
    popupMenu.show();

}

And of course, here's the error log:

07-22 16:40:04.299 24031-24031/com.avena.avenafit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.avena.avenafit, PID: 24031 Theme: themes:{default=overlay:com.materialos.cm.theme, iconPack:com.launchertheme.kxnt.pop, fontPkg:com.materialos.cm.theme, com.android.systemui=overlay:com.materialos.cm.theme, com.android.systemui.navbar=overlay:com.materialos.cm.theme} java.lang.IllegalArgumentException: Expected receiver of type com.avena.avenafit.onboarding.w, but got android.view.ContextThemeWrapper at java.lang.reflect.Method.invoke(Native Method) at android.view.View$DeclaredOnClickListener.onClick(View.java:4453) at android.view.View.performClick(View.java:5204) at android.view.View$PerformClick.run(View.java:21158) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I have also tried setting up an onClickListener on the CardView, but it gives me this error:

07-22 17:08:14.297 8834-8834/com.avena.avenafit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.avena.avenafit, PID: 8834 Theme: themes:{default=overlay:com.materialos.cm.theme, iconPack:com.launchertheme.kxnt.pop, fontPkg:com.materialos.cm.theme, com.android.systemui=overlay:com.materialos.cm.theme, com.android.systemui.navbar=overlay:com.materialos.cm.theme} android.view.InflateException: Binary XML file line #17: Failed to resolve attribute at index 6: TypedValue{t=0x1d/d=0xffffc107 a=3 r=0x7f0d0004} at android.view.LayoutInflater.inflate(LayoutInflater.java:539) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.support.v7.view.menu.w.getView(Unknown Source) at android.support.v7.view.menu.v.i(Unknown Source) at android.support.v7.view.menu.v.f(Unknown Source) at android.support.v7.view.menu.v.d(Unknown Source) at android.support.v7.widget.ea.b(Unknown Source) at com.avena.avenafit.onboarding.x.onClick(Unknown Source) at android.view.View.performClick(View.java:5204) at android.view.View$PerformClick.run(View.java:21158) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x1d/d=0xffffc107 a=3 r=0x7f0d0004} at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:705) at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6890) at android.view.ViewGroup$MarginLayoutParams.(ViewGroup.java:7071) at android.widget.FrameLayout$LayoutParams.(FrameLayout.java:446) at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:386) at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:385) at android.view.LayoutInflater.inflate(LayoutInflater.java:502) at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  at android.support.v7.view.menu.w.getView(Unknown Source)  at android.support.v7.view.menu.v.i(Unknown Source)  at android.support.v7.view.menu.v.f(Unknown Source)  at android.support.v7.view.menu.v.d(Unknown Source)  at android.support.v7.widget.ea.b(Unknown Source)  at com.avena.avenafit.onboarding.x.onClick(Unknown Source)  at android.view.View.performClick(View.java:5204)  at android.view.View$PerformClick.run(View.java:21158)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5461)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Has anyone struggled with this before?

1
Your problem has to do with the theme of your app/the popup.TheAnonymous010
Could you please elaborate on that?herrmartell
The first part of the error is talking about themes and an IllegalArgumentException... java.lang.IllegalArgumentException: Expected receiver of type com.avena.avenafit.onboarding.w, but got android.view.ContextThemeWrapperTheAnonymous010

1 Answers

1
votes

Easiest solution:

Just don't use the onClick attribute in XML, just declare a regular onClickListener for your cardView and add it via setOnClickListener.

Edit: About the other error, this might be helpful: AppCompat PopUp menu RuntimeException The error indicates that there might be some style attribute missing.