本人在使用PopupMenu,遇到的问题,和解决过程

时间:2022-06-24 13:55:55

本人在使用PopupMenu,遇到的问题,和解决过程;

想要达到下拉弹出菜单效果,使用了选项菜单OptionMenu,但是OptionMenu存在使用限制,在sdk版本大于10之后,就无法显示菜单按钮;后来选择下拉菜单PopupMenu,而且PopupMenu的使用方法也很简单;

实现过程代码如下:
一 . 页面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >


<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#303538" >

<ImageView
android:id="@+id/iv_web_menu"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true">

</ImageView >
</RelativeLayout>

</LinearLayout>

二 . Popupmenu布局(文件在menu目录下):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/about"
android:orderInCategory="100"
android:showAsAction="never"
android:title="关于">

</item>
<item
android:id="@+id/exit"
android:orderInCategory="100"
android:showAsAction="never"
android:title="退出">

</item>

</menu>

三 . 实现过程:

    // ImageView 初始化和监听
ImageView menuImageView = (ImageView) findViewById(R.id.iv_web_menu);
menuImageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showMenu(v);
}
});
//PopupMenu 显示
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(getApplicationContext(), v);
popup.getMenuInflater().inflate(R.menu.options, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
}

//PopupMenu 监听
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.about:
showNewDownloadDialog();
return true;
case R.id.exit:
showMultiNewDownloadDialog();
return true;
default:
return false;
}
}

在编译过程中没有出现问题,点击menuImageView 后出错;
出错log如下:

01-09 14:18:56.734: E/AndroidRuntime(16030): FATAL EXCEPTION: main
01-09 14:18:56.734: E/AndroidRuntime(16030): java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute.
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5618)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5759)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:610)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:554)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:56)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.view.LayoutInflater.inflate(LayoutInflater.java:477)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:335)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:190)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:128)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:102)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.support.v7.widget.PopupMenu.show(PopupMenu.java:109)
01-09 14:18:56.734: E/AndroidRuntime(16030): at xino.cs.library.activity.FileDownLoadActivity.showMenu(FileDownLoadActivity.java:145)
01-09 14:18:56.734: E/AndroidRuntime(16030): at xino.cs.library.activity.FileDownLoadActivity$2.onClick(FileDownLoadActivity.java:93)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.view.View.performClick(View.java:4207)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.view.View$PerformClick.run(View.java:17372)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.os.Handler.handleCallback(Handler.java:725)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.os.Looper.loop(Looper.java:137)
01-09 14:18:56.734: E/AndroidRuntime(16030): at android.app.ActivityThread.main(ActivityThread.java:5053)
01-09 14:18:56.734: E/AndroidRuntime(16030): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 14:18:56.734: E/AndroidRuntime(16030): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 14:18:56.734: E/AndroidRuntime(16030): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

错误:
java.lang.RuntimeException: Binary XML file line #17:You must supply a layout_height attribute.

从字面上理解是在xml布局文件中的 line 17 缺少了layout_height属性。但是检查代码发现不存在这个问题,然后去网上查,发现都是跟我想的一样的,没有帮助我解决问题;
本人在使用PopupMenu,遇到的问题,和解决过程
可以从报错中发现是FrameLayout出了问题,然后全局搜索在哪里使用了FrameLayout,并且检查过去,发现只有android.support.v7依赖包中有,然后意识到了了能是导入出错。

把导入路径从:

import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.PopupMenu.OnMenuItemClickListener;

改为:

import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;

就可以了。

总结
错误:
java.lang.RuntimeException: Binary XML file line #17:You must supply a layout_height attribute.
解决方法:
1. 添加xml布局文件layout_height 属性
2. 导入报错误