在操作栏(Android)中点击搜索按钮时崩溃

时间:2016-02-14 02:02:00

标签: java android android-activity android-recyclerview searchview

我一直在关注this tutorial,了解如何在我的活动中添加搜索栏。不幸的是,此时,每次点击搜索图标,应用程序都会立即崩溃并重新启动(因此我无法看到错误)。我可以在他们的代码和我的代码之间看到的唯一区别是,我正在使用标准活动,而他们正在使用片段。我已经尝试相应地更改我的代码。

这是我活动中的代码:

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);
    }
}

在适配器中:

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();
    }
}

我只发布了相关代码。所有必要的方法都在那里。在点击搜索图标之前,一切正常。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

这从未在活动中初始化

private List<Attraction> attractionsList;

此列表作为null传递,很可能导致此处的NullPointerException

adapter.setFilter(attractionsList);

这称之为

public void setFilter(List<Attraction> attractions) {
    attractionsList = new ArrayList<>();
    attractionsList.addAll(attractions);

并且addAll抛出NullPointerException