简体   繁体   中英

Android ListActivity compile error

I am quite new to java android programming and I have a problem that may be quite easy to answer, but I can't figure what the eclipse compile error message implies.

The class runs fine except if I remove the block comment in the onContextItemSelected code below, I get this compile error:

The method onContextItemSelected(MenuItem) of type new View.OnCreateContextMenuListener(){} must override or implement a supertype method

What am I doing wrong or is missing here? Thanks.

Code

public class ListTest<registerForContextMenu> extends ListActivity  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values));
        listView = getListView();

        // I populate the list view here

        listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderIcon(R.drawable.ic_launcher);
                menu.setHeaderTitle(R.string.context_menu_options);
                menu.add(0, 0, 0, R.string.context_menu_reply);
                menu.add(0, 0, 0, R.string.context_menu_delete);
                menu.add(0, 0, 0, R.string.context_menu_cancel);
            };

            /*
            @Override
            public boolean onContextItemSelected(MenuItem item) {
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                int index = info.position;
                return true;
            };
            */                      
       });
    }
}

According to the documentation, onContextItemSelected is not a method of OnCreateContextMenuListener . That means that you cannot override it.

onContextItemSelected is a method of Activity . Override it in your Activity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM