1
I have a Fragmentactivity that calls its Fragments in the shape of tabs (Viewpager).
In two of these fragments the methods were implemented onCreateContextMenu and onContextItemSelected.
In this case, it turns out that when I call the context menu from the list of a fragment and select an item, the method onContextItemSelected called comes from the other fragment. Therefore, any object within that 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 fragment context menu Buying and select an item, for example "Additional Information", the method onContextItemSelected executed is from the fragment Sale. Only this only happens when I do it first by the Vendae fragment right after I call the Purchase fragment. The error that occurs in this scenario is a Nullpointerexception
Thanks @ramaral, but the problem is that it will not perform the right routine. It only prevents you from performing the wrong routine. That is, If in the menu I select Items of the Purchase, it tries to execute the Contextitem of the Sale fragment (Items of the Sale). In that case I need him to go into context, which he’s not doing. Thanks
– raphaRezzi
You tested the solution?
– ramaral
Please note that you must return
false
when it’s not theonContextItemSelected()
intended.– ramaral
had tested yes, but after the last tip that worked. I hadn’t noticed
return false
. Thank you very much. the/– raphaRezzi