我正在尝试使用PopUp菜单创建一个RecyclerView,但每当我尝试访问弹出菜单时,它都会崩溃并说:引起:java.lang.UnsupportedOperationException:无法解析索引1处的属性:TypedValue {t = 0x2 / d = 0x7f01005e a = -1}
我的menu.xml是:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu1"
android:title="Menu 1" />
<item
android:id="@+id/menu2"
android:title="Menu 2" />
<item
android:id="@+id/menu3"
android:title="Menu 3" />
</menu>
style.xml:
<resources>
<!-- Base application theme. -->
<!-- your theme inherits from the material theme -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- theme customizations -->
<!-- Customize your theme here. -->
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">#c21d1111</item>
<item name="colorAccent">#c6897929</item>
</style>
</resources>
适配器类:
/**
* Created by User on 21-Apr-17.
*/
import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<MyList> list;
private Context mCtx;
public CustomAdapter(List<MyList> list, Context mCtx) {
this.list = list;
this.mCtx = mCtx;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_items, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final CustomAdapter.ViewHolder holder, int position) {
MyList myList = list.get(position);
holder.textViewHead.setText(myList.getHead());
holder.textViewDesc.setText(myList.getDesc());
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.option_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
//handle menu1 click
break;
case R.id.menu2:
//handle menu2 click
break;
case R.id.menu3:
//handle menu3 click
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewHead;
public TextView textViewDesc;
public TextView buttonViewOption;
public ViewHolder(View itemView) {
super(itemView);
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
buttonViewOption = (TextView) itemView.findViewById(R.id.textViewOptions);
}
}
}
日志:
04-20 17:36:06.311 10660-10660/com.hridoy.user.ieee_aust D/MediaPlayer: setSubtitleAnchor in MediaPlayer
04-20 17:36:32.171 10660-10675/com.hridoy.user.ieee_aust E/EGL_emulation: tid 10675: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
04-20 17:36:32.171 10660-10675/com.hridoy.user.ieee_aust W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf13f21e0, error=EGL_BAD_MATCH
04-20 17:36:33.147 10660-10660/com.hridoy.user.ieee_aust D/ViewRootImpl[MyOtherActivity]: changeCanvasOpacity: opaque=true
04-20 17:36:33.944 10660-10660/com.hridoy.user.ieee_aust D/ViewRootImpl[MyOtherActivity]: changeCanvasOpacity: opaque=false
04-20 17:36:49.733 10660-10660/com.hridoy.user.ieee_aust D/AndroidRuntime: Shutting down VM
04-20 17:36:49.733 10660-10660/com.hridoy.user.ieee_aust E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hridoy.user.ieee_aust, PID: 10660
android.view.InflateException: Binary XML file line #17: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f01005e a=-1}
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f01005e a=-1}
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:761)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:7035)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7216)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:439)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:370)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:369)
at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:93)
at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160)
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153)
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:141)
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:233)
at com.hridoy.user.ieee_aust.CustomAdapter$1.onClick(CustomAdapter.java:71)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.hridoy.user.ieee_aust"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '25.3.0'
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:design:22.2.0'
testCompile 'junit:junit:4.12'
}
答案 0 :(得分:3)
我通过实例将活动而不是上下文传递给PopupMenu()来解决了这个问题;