2016-07-22 128 views
0

因此,我設置CardView打開PopupMenu,但它只是不斷崩潰,無論我嘗試過。Android PopupMenu不斷崩潰

這裏的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> 

而這裏的方法:

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(); 

} 

當然,這裏的錯誤日誌:

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)

我自己也嘗試設立onClickListener CardView上,但它給了我這個錯誤:

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) 

有沒有人以前與此掙扎?

+0

您的問題與您的應用程序/彈出窗口的主題有關。 – TheAnonymous010

+0

請您詳細說明一下嗎? – herrmartell

+0

錯誤的第一部分是關於主題和IllegalArgumentException ...'java.lang.IllegalArgumentException:預期的接收器類型爲com.avena.avenafit.onboarding.w,但得到了android.view.ContextThemeWrapper' – TheAnonymous010

回答

1

最簡單的辦法:

只是不要在XML使用onclick屬性,只是聲明瞭一個普通onClickListener您cardView並通過setOnClickListener添加它。

編輯:關於其他錯誤,這可能會有所幫助:AppCompat PopUp menu RuntimeException 該錯誤表示可能會丟失某些樣式屬性。

+0

正如你可以在我原來的問題結尾閱讀,我已經試過了。它給出了一個不同的錯誤。我已將它添加到我的帖子中。 – herrmartell

+0

也許這一個幫助:http://stackoverflow.com/questions/28523517/appcompat-popup-menu-runtimeexception?似乎與你的錯誤非常相似。 「無法解析索引6處的屬性」表示缺少某些屬性。 –

+0

@ S.Martell當您通過setOnClickListener設置偵聽器時,您是否刪除了此行「android:onClick =」getGender「」? –