1

我需要在 oncreate 方法中打开上下文菜单。我做什么:

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        cont = new CustomViewContainer(this);
        setContentView(cont);

        this.openContextMenu(cont);
    }
    @Override  
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {  
        super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Select");  
        menu.add(0, v.getId(), 0, "a");  
        menu.add(0, v.getId(), 0, "b");
        menu.add(0, v.getId(), 0, "c");  
    }
}

但是我没有看到菜单。请帮忙!

4

1 回答 1

3

根据文档openContextMenu(View)您必须先致电registerForContextMenu(View)

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    cont = new CustomViewContainer(this);
    setContentView(cont);

    registerForContextMenu(cont);
    openContextMenu(cont);
}
于 2012-12-01T17:34:28.757 回答