I'm trying to hide an image view:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
allImageView = findViewById(R.id.allImageView); // The variable is not null
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if(newText.equals("")) {
allImageView.setVisibility(View.VISIBLE);
} else {
allImageView.setVisibility(View.GONE); // Trying to hide the image view
}
return true;
}
});
}
And I get an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference
error in line allImageView.setVisibility(View.GONE);
Why does this happen? how can I set the visibility?