• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ContextMenu Issue - how to resolve these menu items

kivy

Newbie
Jul 13, 2010
17
0
Hi, I am very new to Android.

I am trying to build inflate a context menu within the sample GridView provided on the Android dev site. Eclipse tells me that the referenced items cannot be resolved, if someone could help find where I coded s.th. wrong, that would be great because I am stuck at the moment. Thanks.

I posted my entire code below. I get the error message at: `if (item.getItemId() == R.id.menu_facebook)` and the other R.id.'s

--------------------------------
This is the ShareGalleryView.java file:


Code:
import android.app.Activity; 
     import android.content.Context; 
     import android.os.Bundle; 
     import android.view.ContextMenu; 
     import android.view.MenuInflater; 
     import android.view.MenuItem; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.view.ContextMenu.ContextMenuInfo;
     import android.widget.BaseAdapter;
     import android.widget.GridView;
     import android.widget.ImageView;
     import android.widget.AdapterView.AdapterContextMenuInfo;
    
     public class ShareGalleryView extends Activity {
       
    	public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.videogrid);
            
            GridView vGrid=(GridView) findViewById(R.id.vgrid);
            registerForContextMenu(vGrid);
            vGrid.setAdapter(new VideoAdapter(this));
        }
    	
    	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	  super.onCreateContextMenu(menu, v, menuInfo);
    	  MenuInflater inflater = getMenuInflater();
    	  inflater.inflate(R.menu.menu_gallery_share, menu);
    	}
      
    	public boolean onContextItemSelected (MenuItem item){
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    		if (item.getItemId() == R.id.menu_facebook)
    		{
    		 //TODO open fb
    			return true;
    			
    		}
    		else if (item.getItemId() == R.id.menu_youtube)
    		{
    			//TODO open youtube
    			return true;
    			
    		}
    		else if (item.getItemId() == R.id.menu_email)
    		{
    		 //TODO open email
    			return true;
    			
    		}
    		else if (item.getItemId() == R.id.menu_bluetooth)
    		{
    			// TODO send via bluetooth
    			return true;
    			
    		}
    	}
    	
        public class VideoAdapter extends BaseAdapter {
        	private Context mContext;
        	
    		public VideoAdapter(Context c) {
    		    mContext = c;
    	    }
    
    	    public int getCount() {
    	        return mThumbIds.length;
    	    }
    
    	    public Object getItem(int position) {
    	        return null;
    	    }
    
    	    public long getItemId(int position) {
    	        return 0;
    	    }
    
    	    // create a new ImageView for each item referenced by the Adapter
    	    public View getView(int position, View convertView, ViewGroup parent) {
    	        ImageView imageView;
    	        if (convertView == null) {  // if it's not recycled, initialize some attributes
    	            imageView = new ImageView(mContext);
    	            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    	            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    	            imageView.setPadding(8, 8, 8, 8);
    	        } else {
    	            imageView = (ImageView) convertView;
    	        }
    
    	        imageView.setImageResource(mThumbIds[position]);
    	        return imageView;
    	    }
    
    	    // references to our images
    	    private Integer[] mThumbIds = {
    	            R.drawable.sample_2, R.drawable.sample_3,
    	            R.drawable.sample_4, R.drawable.sample_2,
    	            R.drawable.sample_6, R.drawable.sample_3,
    	            R.drawable.sample_4, R.drawable.sample_1
    	            
    	    };
        }
    }

and this is the menu_gallery_share.xml file:

Code:
 <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">   
      <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/menu_facebook"
              android:title="@string/Facebook" />
        <item android:id="@+id/menu_youtube"
              android:title="@string/YouTube" />
        <item android:id="@+id/menu_email"
              android:title="@string/Email" />
        <item android:id="@+id/menu_bluetooth"
              android:title="@string/Via Bluetooth" />
       </menu>
    </menu>
 
Hi Kivy,

Is your XML file in a res/menu folder?

If you put it in the wrong place then it won't be valid. (To test that I copied your XML into a file in my res/layout folder, and it gave me errors. But in a res/menu folder it was OK.)

If the XML is invalid the Android dev tools can't generate the code for the R class, which is why you're getting compilation errors.

Look in the gen folder of your project. It should contain a java package, and in there should be an auto-generated R.java file. If that's missing then you know you have an error somewhere. (The errors should also show up in the problems tab/view.)

Mark
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones