0

我要疯了!我有这个来源,但为什么会出错?

这是 MainActivity 的 Menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <group android:id="@+id/group1">
        <item android:id="@+id/item1" android:title="X"></item>
        <item android:id="@+id/item2" android:title="Y"></item>
        <item android:id="@+id/item3" android:title="Z"></item>
        <item android:id="@+id/item4" android:title="Share"></item>
</group>
</menu>

这是Java文件

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_main, menu);
            return true;
        }
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.item1:
                CODE
                }
            return true;
            case R.id.item2:
CODE
            return true;
            case R.id.item3:
CODE
                return true;
            case R.id.item4:
                Intent intent = new Intent(Intent.ACTION_SEND);

                intent.setType("text/plain");

                intent.putExtra(Intent.EXTRA_TEXT, "hXXX");

                intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Check out this site!");

startActivity(Intent.createChooser(intent, "SHARE"));
                return true;
            default:
            return super.onOptionsItemSelected(item);
            }
        }
    }

为什么在这一行给我一个错误:case R.id.item4: The error is Multiple markers at this line - item4 cannot be resolve or is not a field - item4 cannot be resolve or is not a field

4

2 回答 2

0

只需清理您的项目。错误将消失。

于 2013-07-22T06:00:02.173 回答
0

我猜你的 Menu.xml 文件保存在“res/menu/Menu.xml”中。如果是这样,请尝试:

 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.Menu, menu);
        return true;
    }

否则它可能在不同的文件夹中,所以如果您的 Menu.xml 文件在您的布局文件夹中(即“res/layout/Menu.xml”,请尝试:

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.Menu, menu);
        return true;
    }
于 2013-07-22T05:35:25.007 回答