0
The whole problem is happening in a search, when I click and start typing the field it goes filtering 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 charge 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());
}
}
Right on this line
toolbarTitle.setText(restaurant.getNomeRestaurant());
when I’m going 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 only happens when I search the name of the restaurant, otherwise it comes in normally, so if I search it and I go back to the problem.
ATTACHMENTS:
Photo of the debug in the restaurant part = (Restaurant) getArguments(). getSerializable(RESTAURANTE_CARDAPIO);
Make sure that you are enabling the correct XML and that in this XML you have the object with the toolbar_title ID
– Reginaldo Rigo
You are using Activity or Fragment?
– Leonardo Dias
There are some hypotheses, but to actually confirm the verdict, you would have to add more of your code to the question.
– viana
@Leonardodias I am using Fragment
– Renan Rodrigues
@acklay can tell me that add, whatever you need
– Renan Rodrigues
For example,
restaurante
, does not have at any time where it is being declared (in question) or receiving its constructor. This may even be a cause to the problem.– viana
Renan, I added an answer, give a look! Hugs
– Leonardo Dias
@Leonardodias and in the case of restaurant duplication in the search list
– Renan Rodrigues
You have to debug to see if you’re really assigning value to
restaurante
through the(Restaurante) getArguments().getSerializable(RESTAURANTE_CARDAPIO);
– viana
@acklay debugged and this coming normally, ta problem at set time.
– Renan Rodrigues
Put it this way:
toolbarTitle.setText(" "+restaurante.getNomeRestaurante())
... and says what happens.– viana
@acklay in the same way
– Renan Rodrigues
@acklay was here thinking, if I redirect to the restaurant listing fragment, what do you think
– Renan Rodrigues
@Renanrodrigues I wish I could help you, but there is no way to reproduce your error here because the code is not complete, both xml, and your class. I did not understand very well what would be "redirect to the fragment of listing of restaurants".... The answers you have been given are valid, but for some cases. Basically they are answers that suppose an error, they are "kicking". I could kick something, but there are numerous possibilities of error. = D
– viana
I managed to reach something, I verified that what is happening is when I research it is not finding null, however what you think of geviewbyid is another value, I’m still investigating here
– Renan Rodrigues
Let’s go continue this discussion in chat.
– Renan Rodrigues