Applying a custom header for ContextMenu

倖福魔咒の 提交于 2019-12-12 04:57:17

问题


I'm trying to apply a custom header to the ContextMenu of a ListView. Here's the code.

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    View header = View.inflate(getSherlockActivity(),
            R.id.context_menu_header, (ViewGroup) v);
    TextView title = (TextView) header
            .findViewById(R.id.context_menu_title);
    title.setText(cursor.getString(1));
    menu.setHeaderView(header);

    android.view.MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

This code fails with a Resources$NotFoundException: Resource ID #0x7f050038 type #0x12 is not valid. In the line View header = View.inflate(getSherlockActivity(), R.id.context_menu_header, (ViewGroup) v);. I'm guessing probably because of param (ViewGroup) v. How can I get around this error?


回答1:


I was using R.id.context_menu_header instead of a R.layout.layout_file and I had to pass null for the last parameter of the View.inflate method.




回答2:


My first though is an error in one of your XML-files. Some ID that is not defined correctly or something. It would be helpful if you would post some more code including the XML-file :)



来源:https://stackoverflow.com/questions/11205812/applying-a-custom-header-for-contextmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!