findViewById в раздутом меню - PullRequest
0 голосов
/ 27 ноября 2011

В следующем коде 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 / menu / 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>

Я пытался «очистить» (Project -> Cleanиз главного меню затмения) проект, но он все еще не работает.

С уважением, ProgVal

Ответы [ 2 ]

1 голос
/ 27 ноября 2011

Вы не можете получить пункт меню с помощью findViewById (); Вместо этого вы должны использовать

@Override
public boolean onContextItemSelected(MenuItem item) {
   if (item.getItemId() == android.R.id.showuser_contextmenu_showthread)
      //Your stuff
      return true;
   }
   return super.onContextItemSelected(item);
}
1 голос
/ 27 ноября 2011

попробуй:

View button = menu.findViewById(R.id.showuser_contextmenu_showthread);

вместо:

View button = this.findViewById(R.id.showuser_contextmenu_showthread);
...