Error Search View in Custom Adapter

Asked

Viewed 88 times

0

Hello, I’m having a hell of a doubt how to put a search view on a custom Adapter.. I’ve been trying for days and I can’t. This following code that I am using is giving error when starting, I followed several identical tutorials but mine will not. When I use the " setupSearchView();" the app does not open, and gives the following error in the log "java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Searchview.setIconifiedByDefault(Boolean)' on a null Object Reference"

Activity Mediaball.

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import java.util.ArrayList;
import static escompany.pxgguide.R.drawable.fire;

public class MediaBall extends AppCompatActivity implements SearchView.OnQueryTextListener {


    private SearchView mSearchView;
    ListView lista;
    public ArrayList<Pokemon> adicionarEscolas;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_ball);

         lista = (ListView) findViewById(R.id.pokemons);
        adapter = new PokemonAdapter(this, adicionarEscolas());
        lista.setAdapter(adapter);

        lista.setTextFilterEnabled(true);
       setupSearchView();

    mSearchView=(SearchView) findViewById(R.id.search15);

        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // When clicked, show a toast with the TextView text
                if(position == 0) {
                    //code specific to first list item
                    Intent myIntent = new Intent(view.getContext(), bulbasaur.class);
                    startActivityForResult(myIntent, 0);
                }
                if(position == 1) {
                    //code specific to first list item
                    Intent myIntent = new Intent(view.getContext(), bulbasaur.class);
                    startActivityForResult(myIntent, 0);
                }

                if(position == 2) {
                    //code specific to 2nd list item
                    Intent myIntent = new Intent(view.getContext(), bulbasaur.class);
                    startActivityForResult(myIntent, 0);
                }
            }
        });


    }


    public ArrayList<Pokemon> adicionarEscolas() {
        ArrayList<Pokemon> escolas = new ArrayList<Pokemon>();
        Pokemon
        e = new Pokemon("Bulbasaur", "#001", R.drawable.bulbasaur, R.drawable.type_grass, R.drawable.type_poison,"20");
        escolas.add(e);
        e = new Pokemon("Ivysaur", "#002", R.drawable.ivysaur,R.drawable.type_grass, R.drawable.type_poison,"40");
        escolas.add(e);
        e = new Pokemon("Venusaur", "#003", R.drawable.venusaur,R.drawable.type_grass, R.drawable.type_poison,"80");
        escolas.add(e);
        e = new Pokemon("Charmander", "#004", R.drawable.charmander, fire, fire,"20");
        escolas.add(e);
        e = new Pokemon("Charmeleon", "#005", R.drawable.charmeleon, fire, fire,"40");
        escolas.add(e);
return escolas;
    }

    @Override
    public void onBackPressed() {



            super.onBackPressed();
        }
 private void setupSearchView()
   {
       mSearchView.setIconifiedByDefault(false);
       mSearchView.setOnQueryTextListener(this);
       mSearchView.setSubmitButtonEnabled(true);
       mSearchView.setQueryHint("Search Here");
   }
    @Override
    public boolean onQueryTextChange(String newText)
    {

        if (TextUtils.isEmpty(newText)) {
            lista.clearTextFilter();
        } else {
            lista.setFilterText(newText);
        }
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query)
    {
        return false;
    }


}

Pokemonadapter

public class PokemonAdapter extends ArrayAdapter<Pokemon> {
private final Context context;
private final ArrayList<Pokemon> elementos;

public PokemonAdapter(Context context, ArrayList<Pokemon> elementos){
    super(context,R.layout.linha,elementos);
    this.context = context;
    this.elementos = elementos;

}



@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public View getView(int position, View convertView, ViewGroup parent){

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.linha, parent, false);
    TextView nomeEscola = (TextView) rowView.findViewById(R.id.nome);
    TextView endereco= (TextView) rowView.findViewById(R.id.endereço);
    ImageView imagem = (ImageView) rowView.findViewById(R.id.imagem);
    ImageView imagetype1 = (ImageView) rowView.findViewById(R.id.imagemtype1);
    ImageView imagetype2 = (ImageView) rowView.findViewById(R.id.imagemtype2);
    TextView level = (TextView) rowView.findViewById(R.id.level);

    nomeEscola.setText(elementos.get(position).getNome());
    endereco.setText(elementos.get(position).getEndereco());
    imagem.setImageResource(elementos.get(position).getImagem());
    imagetype1.setImageResource(elementos.get(position).getImagemtype1());
    imagetype2.setImageResource(elementos.get(position).getImagemtype2());
    level.setText(elementos.get(position).getLevel());


    return rowView;



} }

Pokemon

public class Pokemon {
private String nome;
private int imagem;
private String endereco;
private int imagemtype1;
private int imagemtype2;
private String level;

public Pokemon(String nome, String endereco, int imagem, int imagemtype1, int imagemtype2, String level){
    this.nome = nome;
    this.endereco = endereco;
    this.imagem = imagem;
    this.imagemtype1 = imagemtype1;
    this.imagemtype2 = imagemtype2;
    this.level = level;
}

public Pokemon(MediaBall mediaBall) {


}


public String getNome() {  return nome;    }

public void setNome(String nome) {
    this.nome = nome;
}

public int getImagem() {
    return imagem;
}

public void setImagem(int imagem) {
    this.imagem = imagem;
}

public String getEndereco() {
    return endereco;
}

public void setEndereco(String endereco) {
    this.endereco = endereco;
}

public int getImagemtype1() {
    return imagemtype1;
}

public void setImagemtype1(int imagemtype1) {
    this.imagemtype1 = imagemtype1;
}

public int getImagemtype2() {
    return imagemtype2;
}

public void setImagemtype2(int imagemtype2) {
    this.imagemtype2 = imagemtype2;
}

public String getLevel() {
    return level;
}

public void setLevel(String level) {
    this.level = level;
} }

Xml

<?xml version="1.0" encoding="utf-8"?>

tools:context="escompany.pxgguide.MediaBall">


<SearchView
    android:id="@+id/search15"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

</SearchView>

<ListView
    android:id="@+id/pokemons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/search15"
    android:layout_centerHorizontal="true"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"
    />

1 answer

1


Invert these lines in onCreate:

setupSearchView();

mSearchView=(SearchView) findViewById(R.id.search15);

Thus remaining:

mSearchView=(SearchView) findViewById(R.id.search15);      

setupSearchView();

Because you are trying to use a Searchview method before it is initialized, so the null error Object Reference.

  • Thanks man, it worked here. only this not showing the results when searching in search.. but thank you :)

  • Ball show, don’t forget to accept the answer. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.