I have been following this tutorial to find out how to add a search bar to my activity. Unfortunately at the moment, each time I tap on the search icon, the app crashes and restarts immediately (so I can't see an error). The only difference I can see between their code and mine, is that I am using a standard activity, whereas they are using fragments. I have tried to change my code accordingly.
This is the code in my activity:
public class AttractionsListActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
public static AttractionRowAdapter adapter;
private RecyclerView mRecyclerView;
public static SwipeRefreshLayout swipeContainer;
public static Park parkPassed;
private List<Attraction> attractionsList;
@Override
protected void onPause() {
super.onPause();
adapter.clearAdaptor();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attractions_list);
final Intent intent = getIntent();
parkPassed = intent.getParcelableExtra("parkPassed");
DataManager.loadAttractions(getBaseContext(), parkPassed.name.replaceAll("\\s+",""));
swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
DataManager.loadAttractions(getBaseContext(), parkPassed.name.replaceAll("\\s+",""));
adapter.clearAdaptor();
}
});
swipeContainer.setColorSchemeColors(Color.parseColor("#FF2F92"),
Color.parseColor("#0080FF"));
this.setupRecycler();
this.setupImageLoader();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_search, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adapter.setFilter(attractionsList);
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onQueryTextChange(String newText) {
final List<Attraction> filteredModelList = filter(attractionsList, newText);
adapter.setFilter(filteredModelList);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<Attraction> filter(List<Attraction> models, String query) {
query = query.toLowerCase();
final List<Attraction> filteredModelList = new ArrayList<>();
for (Attraction model : models) {
final String text = model.name.toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
private void setupRecycler() {
mRecyclerView = (RecyclerView) findViewById(R.id.attractions_recycler);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
adapter = new AttractionRowAdapter(AttractionsListActivity.this, DataManager.attractionArrayList);
mRecyclerView.setAdapter(adapter);
}
}
And in the adapter:
public class AttractionRowAdapter extends RecyclerView.Adapter<AttractionRowHolder> {
private List<Attraction> attractionsList;
private Context context;
public AttractionRowAdapter(Context context, List<Attraction> attractionsArrayList) {
this.attractionsList = attractionsArrayList;
this.context = context;
}
@Override
public int getItemCount() {
return (null != attractionsList ? attractionsList.size() : 0);
}
public void setFilter(List<Attraction> attractions) {
attractionsList = new ArrayList<>();
attractionsList.addAll(attractions);
notifyDataSetChanged();
}
}
I've only posted the relevant code. All of the necessary methods are there. Everything works fine up until I tap the search icon.
Any ideas?
This is never initialized in the Activity
private List<Attraction> attractionsList;
This list is being passed around as null, and most likely results in a NullPointerException at this
adapter.setFilter(attractionsList);
Which calls this
public void setFilter(List<Attraction> attractions) {
attractionsList = new ArrayList<>();
attractionsList.addAll(attractions);
And addAll
throws a NullPointerException