Click here to Skip to main content
16,022,131 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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;


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:

MSIL
<?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 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;
    }
}
Posted
Comments
Obaid Ahmed 24-Feb-11 1:55am    
there is no error in code as well in XML,
some time R.java behave abnormal, try this
1) save the project
2) then close project
3) then again reopen,

by this some times R.java auto creates your desire id.
Willtwinny 24-Feb-11 2:02am    
Thank-you for your reply.
I just realised it was Eclipse 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.

Thank-you for your speedy reply and sorry to have troubled you.
Obaid Ahmed 24-Feb-11 2:11am    
lol :) that's usually happening with everyone, when any one new for android environment ;)
Willtwinny 24-Feb-11 2:26am    
lol yeah I'm new and I am glad I am not the only one that this has happened to :)
Thanks once again!
Obaid Ahmed 24-Feb-11 2:46am    
you are most welcome :)

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
 
Share this answer
 
Comments
Willtwinny 24-Feb-11 2:08am    
Yeah, that did seem to be the case after when I resolved the icon errors - eclipse automatically resolved them for me.
For some reason eclipse didn't want to suggest that until I put the icons in the drawable folder.

I feel stupid now.
Thanks for your reply :-)
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900