The problem is all happening in a search, when I click and start typing the field it will filter the results, when I click on one of these results to open another screen, this error:
Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference
The search code looks like this:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_restaurante, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getActivity()
.getSystemService(Context.SEARCH_SERVICE);
searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
if(restaurantes == null || restaurantes.size() == 0){
searchView.setEnabled(false);
}
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getActivity().getComponentName()));
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
return false;
}
@Override
public boolean onQueryTextChange(String arg0) {
filtrarRestaurante(arg0);
return false;
}
});
}
//searchView.setVisibility(View.INVISIBLE);
super.onCreateOptionsMenu(menu, inflater);
}
The function filtarRestaurante(agr0)
is this:
public void filtrarRestaurante(String filtro){
ArrayList<Restaurante> restaurantesEncontrados = new ArrayList<Restaurante>();
if(filtro.length() == 0 && restaurantes != null){
restaurantes.clear();
restaurantes.addAll(restaurantesBkp);
configurarAdapter();
return;
}
// Procura pelo restaurante
restaurantes.clear();
restaurantes.addAll(restaurantesBkp);
for(Restaurante restaurante : restaurantes){
if(restaurante.getNomeRestaurante().toLowerCase().contains(filtro.toLowerCase())){
restaurantesEncontrados.add(restaurante);
}
}
restaurantes = restaurantesEncontrados;
configurarAdapter();
}
Here is the init of my fragment:
@AfterViews
public void init() {
Globales.setPedido(new Pedido());
restaurante = (Restaurante) getArguments().getSerializable(RESTAURANTE_CARDAPIO);
Globales.setRestauranteAtual(restaurante);
carregarCardapio(restaurante);
}
So I instantiate the restaurant:
Restaurante restaurante;
This error is happening when I click on the restaurant and the error happens here:
@Override
public void onResume() {
super.onResume();
try{
updateCarrinho();
Globales.setListaOpcionais(null);
TextView toolbarTitle = (TextView) getActivity().findViewById(
R.id.toolbar_title);
toolbarTitle.setText(restaurante.getNomeRestaurante());
}catch (Exception e){
MessageUtil.showError(getActivity(), e.getMessage());
}
}
Exactly on this line
toolbarTitle.setText (restaurant.getRestaurantName ());
When I go to name the restaurant the exception, why would I give it?
- I've noticed that restaurants sometimes double in search results.
-
The cool thing is that this happens only when I look up the restaurant name, otherwise it comes in normally, so if I search for it and re-enter the problem.
ATTACHMENTS:
Photo of debug in restaurant part = (Restaurant) getArguments (). getSerializable (RESTAURANT_CARDAPIO);