安卓菜单问题
我正在尝试创建一个菜单项,以便当用户单击手机上的菜单按钮时,它会显示此菜单。我的代码正在编译,它显示一个菜单,但不显示与菜单按钮关联的图像或文本。
我将图像放在文件夹 res/drawable/inage1icon.png 中 知道问题是什么吗?
下面是代码
package com.webview;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.view.Menu;
import android.view.MenuInflater;
public class WebViewActivity extends Activity {
WebView mWebView;
public boolean onCreateoptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu)
return true;
}
}
mainmenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/buttoneone"
android:icon="@drawable/image1icon"
android:title="@string/showimage1" />
</menu>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, WebViewActivity!</string>
<string name="app_name">WebView</string>
<string name="showimage1">IMAGE ONE</string>
<color name="background">#000000</color>
</resources>
Im trying to create a menu item so when a user clicks on the menu button on their phone it displays this menu. My code is compiling and its displaying a menu but not the image or text assoiciated with the menu button.
I have the image in a folder res/drawable/inage1icon.png
Any Idea what the issue is?
Below is the code
package com.webview;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.view.Menu;
import android.view.MenuInflater;
public class WebViewActivity extends Activity {
WebView mWebView;
public boolean onCreateoptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu)
return true;
}
}
mainmenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/buttoneone"
android:icon="@drawable/image1icon"
android:title="@string/showimage1" />
</menu>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, WebViewActivity!</string>
<string name="app_name">WebView</string>
<string name="showimage1">IMAGE ONE</string>
<color name="background">#000000</color>
</resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是复制和粘贴错误,还是您的代码在应用程序中的方式?
您拼错了方法名称。它应该是:
而您有
public boolean onCreateoptionsMenu(Menu menu)
。另外,返回超类的方法也是一个好主意;不要说return true
,而是说return(super.onCreateOptionsMenu())
。编辑:此外,如果您在 Eclipse 中进行开发,如果您使用快捷键 Cmd+Opt+s 并选择
覆盖/实现方法
,则可以确保不会发生此类拼写错误。在该菜单中,Eclipse 将列出您扩展的类中的所有方法。Is this a copying and pasting error, or is this the way your code is within the app?
You've misspelled the method name. It should be:
whereas you have
public boolean onCreateoptionsMenu(Menu menu)
. Also, it is a good idea to return the superclass's method; instead of sayingreturn true
, sayreturn(super.onCreateOptionsMenu())
.EDIT: Also, if you are developing in Eclipse, you can ensure that spelling errors such as this won't occur if you use the shortcut Cmd+Opt+s and select
Override/Implement Methods
. In that menu, Eclipse will list all the methods in the class you have extended.