我正在使用自定义适配器并在getView()方法上添加此代码:
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();
}
});
但是我收到了这个错误:
java.lang.RuntimeException: Failed to resolve attribute at index 6
相同的代码适用于ListItemClick(),但它没有意义,因为需要单击一次进行设置并再次单击以触发PopupMenu。
修改: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)
答案 0 :(得分:5)
通过以下导入,我总是遇到错误:
import android.support.v7.widget.PopupMenu;
使用以下导入工作正常:
import android.widget.PopupMenu;
答案 1 :(得分:0)
我这样解决:
在适配器getView()上添加它:
ImageView popupMenu = (ImageView) v.findViewById(R.id.popupMenu);
popupMenu.setTag(getItem(position));
popupMenu.setOnClickListener(MyFragment.popupMenuListener);
这就在MyFragment上:
implements OnClickListener
popupMenuListener = this;
@Override
public void onClick(final View view) {
view.post(new Runnable() {
@Override
public void run() {
showPopupMenu(view);
}
});
}
答案 2 :(得分:0)
此问题与缺少主题属性有关
确保您的应用主题扩展为Theme.AppCompat
。
<style name="AppTheme" parent="@style/Theme.AppCompat" />