Android菜单无法正常工作-请帮助 [英] Android Menu not working - Please help

查看:67
本文介绍了Android菜单无法正常工作-请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码引发错误,我不确定原因:

This code brings up an error and I am unsure why:

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.Friends:
            // TODO
            break;



错误显示R.id无法解析
它带来了2个修复程序,但是它们都编辑了R.java文件,我已被指示不要乱用.

我只是想知道如何解决这个问题.
请帮忙,谢谢.

我的代码:

XML文件:



The error says R.id cannot be resolved
It brings up 2 fixes, but they both edit the R.java file which I have been instructed not to mess about with.

I am just wondering what I can do to resolve it.
Please help, thanks.

My Code:

XML file:

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

<item android:id="@+id/friends"
    android:title="Friends"
    android:icon="@drawable/friends" >
</item>

<item android:id="@+id/events"
    android:title="Events"
    android:icon="@drawable/events" >
</item>

<item android:id="@+id/Profile"
    android:title="Profile"
    android:icon="@drawable/profile" >
</item>

<item android:id="@+id/Close"
    android:title="Close"
    android:icon="@drawable/close" >
</item>
</menu>




Java文件:




Java File:

package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.app_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.Friends:
            // TODO
            break;
        case R.id.Events:
            // TODO
            break;
            case R.id.Profile:
            // TODO
            break;
        case R.id.Close:
            // TODO
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }
}

推荐答案

R.id还是R.id.Friends无法解决?
看来您应该使用R.id.friends代替,并使用小写字母F.

希望这会有所帮助,
Fredrik
Is it R.id or R.id.Friends that can''t be resolved?

It looks like you should have used R.id.friends instead, with a lower case F.

Hope this helps,
Fredrik


我刚刚意识到这是Eclipse IDE发挥了作用.
我一直等到完成编码后才放入菜单的图标文件.
将图标放在drawable文件夹中后,错误已解决.
I just realised it was Eclipse IDE acting up.
I was waiting until I finished coding before putting in the icon files for the menu.
Once I placed the icons in the drawable folder the errors were resolved.


这篇关于Android菜单无法正常工作-请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆