[英]Crash when tapping search button in action bar (Android)
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 这从未在Activity中初始化
private List<Attraction> attractionsList;
This list is being passed around as null, and most likely results in a NullPointerException at this 此列表以null形式传递,很可能在此导致NullPointerException
adapter.setFilter(attractionsList);
Which calls this 哪个叫这个
public void setFilter(List<Attraction> attractions) {
attractionsList = new ArrayList<>();
attractionsList.addAll(attractions);
And addAll
throws a NullPointerException 并且
addAll
抛出NullPointerException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.