Android Menu not showing up

家住魔仙堡 提交于 2019-12-08 07:36:26

问题


Im trying to get a custom menu to show when the menu button is clicked on my phone. Its not showing at all.

I have a register icon caled register.png in this folder /res/drawable. I have my my_menu.xml in a folder called /res/menu. Did I lay out my folders wrong or is there something wrong in my code below.

I renamed menu.xml to my_menu.xml I changed my code and now im getting these errors:

[2012-04-07 07:50:43 - HelloWebView] W/ResourceType( 1560): Bad XML block: no root element node found [2012-04-07 07:50:43 - HelloWebView] C:\Users\josh\workspace\HelloWebView\res\menu\my_menu.xml:4: error: No resource identifier found for attribute 'showAsAction' in package 'android'

my_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:id="@+id/register"          
        android:icon="@drawable/register"          
        android:title="@string/register"          
        android:showAsAction="ifRoom"/>    

</menu>

Mainapp

public class HelloWebViewActivity extends Activity {
    WebView mWebView;

    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);    
        mWebView = (WebView) findViewById(R.id.webview);    
        mWebView.getSettings().setJavaScriptEnabled(true);    
        mWebView.loadUrl("http://www.Google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());
    }

    private class HelloWebViewClient extends WebViewClient {   
        @Override    
        public boolean shouldOverrideUrlLoading(WebView view, String url) {        
            view.loadUrl(url);        
            return true;    
        }}

    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {    
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {        
            mWebView.goBack();       
            return true;    
        }    
        return super.onKeyDown(keyCode, event);}

    @Override
    public boolean onCreateOptionsMenu(Menu my_menu) {    
    MenuInflater inflater = getMenuInflater();    
    inflater.inflate(R.menu.menu, my_menu);    

    return true;
    }
}

回答1:


inflater.inflate(R.menu.my_menu, my_menu); 

That solved the code alone with removing
android:showAsAction="ifRoom"




回答2:


I had a similar problem. I did not get any error, just the menu button didn't show up. I fixed the problem in Manifest.xml file by changing android:theme="@style/AppBaseTheme" (or any other theme compatible with minSDK) . Because I was messing with style.xml file and created my own. This caused the problem. May help.




回答3:


Every thing look clean just clean and build your app. And also if these does not solve your problem once unistall the app and reinstall it.

also if your xml name is Menu.xml make it menu.xml... that is case sensitive



来源:https://stackoverflow.com/questions/10051104/android-menu-not-showing-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!