I have a FragmentActivity that calls its Fragments tabbed (ViewPager).
In both of these fragments the onCreateContextMenu and onContextItemSelected methods were implemented.
In the case, it happens that when I call the context menu from a fragment's list and select an item, the called onContextItemSelected method comes from the other fragment. Therefore, any object within this method is null and generates the exception.
Fragment 1 - Purchase
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 0, 0, "Informações adicionais");
menu.add(0, 1, 1, "Itens da Compra");
menu.add(0, 2, 2, "Formas de Pagamento");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
dialog = new Dialog(getActivity());
switch (item.getItemId()) {
case 0:
dialog.setContentView(R.layout.item_for_listview_consulta_compras);
dialog.setTitle("Informações Adicionais");
exibeCompra(arrayCompra.getItem(info.position));
break;
case 1:
dialog.setContentView(R.layout.simple_listview);
dialog.setTitle("Itens da Compra");
exibeListaItens(arrayCompra.getItem(info.position));
break;
case 2:
dialog.setContentView(R.layout.recebimento_forma_pagamento_layout);
dialog.setTitle("Formas de Pagamento");
exibeFormaPagamento(arrayCompra.getItem(info.position));
break;
default:
return false;
}
return true;
}
Fragment 2 - Sale
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 0, 0, "Informações adicionais");
menu.add(0, 1, 1, "Itens da Venda");
menu.add(0, 2, 2, "Formas de Pagamento");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
dialog = new Dialog(getActivity());
switch (item.getItemId()) {
case 0:
dialog.setContentView(R.layout.item_for_consulta_venda);
dialog.setTitle("Informações Adicionais");
exibeVenda(arrayInfoVendas.getItem(info.position));
break;
case 1:
dialog.setContentView(R.layout.simple_listview);
dialog.setTitle("Itens da Venda");
exibeListaItens(arrayInfoVendas.getItem(info.position));
break;
case 2:
dialog.setContentView(R.layout.recebimento_forma_pagamento_layout);
dialog.setTitle("Formas de Pagamento");
exibeFormaPagamento(arrayInfoVendas.getItem(info.position));
break;
default:
return false;
}
return true;
}
FragmentActivity
public class ConsultaTab extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabPagerAdapter myAdapter;
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
Funcoes.setMenuOpcoesSempreOn(getApplicationContext());
setContentView(R.layout.act_consulta_cliente_tab);
setTitle(R.string.title_activity_consulta);
myAdapter = new TabPagerAdapter(getSupportFragmentManager());
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(myAdapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
if (extras != null)
codPessoa = extras.getLong("codPessoaErp");
for (int i = 0; i < myAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(myAdapter.getPageTitle(i)).setTabListener(this));
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
When I call the context menu of the Buy fragment and select an item, for example, "Additional information", the onContextItemSelected method is run from the Sale . But this only happens when I do this first by the fragment Vendae and soon after I call the fragment Buy. The error that occurs in this scenario is a NullPointerException