ContextMenu Issue - how to resolve these menu items


Last Updated:

  1. kivy

    kivy Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    17
    Likes Received:
    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 (Text):
    1. import android.app.Activity;
    2.      import android.content.Context;
    3.      import android.os.Bundle;
    4.      import android.view.ContextMenu;
    5.      import android.view.MenuInflater;
    6.      import android.view.MenuItem;
    7.      import android.view.View;
    8.      import android.view.ViewGroup;
    9.      import android.view.ContextMenu.ContextMenuInfo;
    10.      import android.widget.BaseAdapter;
    11.      import android.widget.GridView;
    12.      import android.widget.ImageView;
    13.      import android.widget.AdapterView.AdapterContextMenuInfo;
    14.    
    15.      public class ShareGalleryView extends Activity {
    16.        
    17.         public void onCreate(Bundle savedInstanceState) {
    18.             super.onCreate(savedInstanceState);
    19.             setContentView(R.layout.videogrid);
    20.            
    21.             GridView vGrid=(GridView) findViewById(R.id.vgrid);
    22.             registerForContextMenu(vGrid);
    23.             vGrid.setAdapter(new VideoAdapter(this));
    24.         }
    25.        
    26.         public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    27.           super.onCreateContextMenu(menu, v, menuInfo);
    28.           MenuInflater inflater = getMenuInflater();
    29.           inflater.inflate(R.menu.menu_gallery_share, menu);
    30.         }
    31.      
    32.         public boolean onContextItemSelected (MenuItem item){
    33.         AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    34.             if (item.getItemId() == R.id.menu_facebook)
    35.             {
    36.              //TODO open fb
    37.                 return true;
    38.                
    39.             }
    40.             else if (item.getItemId() == R.id.menu_youtube)
    41.             {
    42.                 //TODO open youtube
    43.                 return true;
    44.                
    45.             }
    46.             else if (item.getItemId() == R.id.menu_email)
    47.             {
    48.              //TODO open email
    49.                 return true;
    50.                
    51.             }
    52.             else if (item.getItemId() == R.id.menu_bluetooth)
    53.             {
    54.                 // TODO send via bluetooth
    55.                 return true;
    56.                
    57.             }
    58.         }
    59.        
    60.         public class VideoAdapter extends BaseAdapter {
    61.             private Context mContext;
    62.            
    63.             public VideoAdapter(Context c) {
    64.                 mContext = c;
    65.             }
    66.    
    67.             public int getCount() {
    68.                 return mThumbIds.length;
    69.             }
    70.    
    71.             public Object getItem(int position) {
    72.                 return null;
    73.             }
    74.    
    75.             public long getItemId(int position) {
    76.                 return 0;
    77.             }
    78.    
    79.             // create a new ImageView for each item referenced by the Adapter
    80.             public View getView(int position, View convertView, ViewGroup parent) {
    81.                 ImageView imageView;
    82.                 if (convertView == null) {  // if it's not recycled, initialize some attributes
    83.                     imageView = new ImageView(mContext);
    84.                     imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    85.                     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    86.                     imageView.setPadding(8, 8, 8, 8);
    87.                 } else {
    88.                     imageView = (ImageView) convertView;
    89.                 }
    90.    
    91.                 imageView.setImageResource(mThumbIds[position]);
    92.                 return imageView;
    93.             }
    94.    
    95.             // references to our images
    96.             private Integer[] mThumbIds = {
    97.                     R.drawable.sample_2, R.drawable.sample_3,
    98.                     R.drawable.sample_4, R.drawable.sample_2,
    99.                     R.drawable.sample_6, R.drawable.sample_3,
    100.                     R.drawable.sample_4, R.drawable.sample_1
    101.                    
    102.             };
    103.         }
    104.     }
    and this is the menu_gallery_share.xml file:

    Code (Text):
    1.  <?xml version="1.0" encoding="utf-8"?>
    2.     <menu xmlns:android="http://schemas.android.com/apk/res/android">  
    3.       <menu xmlns:android="http://schemas.android.com/apk/res/android">
    4.         <item android:id="@+id/menu_facebook"
    5.               android:title="@string/Facebook" />
    6.         <item android:id="@+id/menu_youtube"
    7.               android:title="@string/YouTube" />
    8.         <item android:id="@+id/menu_email"
    9.               android:title="@string/Email" />
    10.         <item android:id="@+id/menu_bluetooth"
    11.               android:title="@string/Via Bluetooth" />
    12.        </menu>
    13.     </menu>
    14.  
     

    Advertisement
  2. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
  3. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    Make sure the xml file is saved as well. But definitely check the R file and see if what you think should be in there is.
     
  4. kivy

    kivy Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    17
    Likes Received:
    0
    I found the problem...within the xml file I had to change the "@string/Facebook" into simply "Facebook" :p
     

Share This Page

Loading...