views:

75

answers:

1

I followed the instructions and examples on Android website to create a context menu but mine shows up completely black and i cannot change any options in it ; anyone out there had the same experience and can help me solve this issue.

FWIW, here are my class .java and the menu .xml files

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <group android:checkableBehavior="single">
        <item android:id="@+id/red"
              android:title="@string/red"
              android:checked="true" />
        <item android:id="@+id/blue"
              android:title="@string/blue" />
        <item android:id="@+id/green"
              android:title="@string/green" />
        <item android:id="@+id/yellow"
              android:title="@string/yellow" />
        <item android:id="@+id/black"
              android:title="@string/black" />
        <item android:id="@+id/white"
              android:title="@string/white" />
        <item android:id="@+id/orange"
              android:title="@string/orange" />
    </group>
</menu>

 

package com.MyProject;

import android.view.MenuInflater;
import android.view.MenuItem;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;

public class ColorsActivity extends Activity {

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
          super.onCreateContextMenu(menu, v, menuInfo);
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.context_menu, menu);
          registerForContextMenu(v);

    }
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.red:
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            case R.id.blue:
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            case R.id.green:
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            case R.id.yellow:
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            case R.id.black:
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            case R.id.white:
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            case R.id.orange:
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
}
+2  A: 

Activity.registerForContextMenu (View) registers the context menu with the OS so that when the menu button is pressed and the given view is in the foreground, the callback to onCreateContextMenu is made. What you have done is register the view within the callback, making it fundamentally unreachable in your code because the view would have to be already registered in order to reach the registration you have here. registerForContextMenu should be called in one of your lifecycle startup methods, probably onResume.

Sam Svenbjorgchristiensensen
I made a mistake in my answer, but i've now edited it so hopefully you're not too confused.
Sam Svenbjorgchristiensensen