0
在下面的代码,findViewById返回null:findViewById虚增菜单
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
if (view == this.getListView()) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
final Message clickedMessage = this.adapter.getItem(info.position);
menu.setHeaderTitle(clickedMessage.getTitle());
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.showuser_context, menu);
View button = this.findViewById(R.id.showuser_contextmenu_showthread);
// I would like to call button.setOnClickListener here
}
而RES /菜单/ showuser_context.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/showuser_contextmenu_showthread" android:title="@string/showuser_contextmenu_showthread"></item>
<item android:id="@+id/showuser_contextmenu_reply" android:title="@string/showuser_contextmenu_reply"></item>
</menu>
我tryed为 “干净”(项目 - >清除从eclipse主菜单)该项目,但它仍然无法正常工作。
问候, ProgVal
好主意,但它仍然返回null。 'view'代替'this'。 –
从逻辑上讲,它应该有效,因为你已经在你的菜单中夸大了你的xml,所以当你尝试使用它来查找视图时,它会搜索你的Activity的ContentView上的视图(而不是菜单),这就是为什么你应该在你的菜单实例中找到你的意见 – Houcine