-1

i'm trying to make the search for custom listview, but i got some trouble with the NullPointerException

in there is an error in line76, logcat:

java.lang.NullPointerException
                                                                              at com.aeu.mlibrary.mlibraryaeu.SearchFragment.onCreateOptionsMenu(SearchFragment.java:76)

when i check in line 76 this is the code:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.activity_main_actions, menu);

    MenuItem myActionMenuItem = menu.findItem( R.id.item_search);
    final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (TextUtils.isEmpty(newText)) {
                adapter.filter("");
                lvSearch.clearTextFilter();
            } else {
                adapter.filter(newText);
            }
            return true;
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}

line 76 is:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

and this is my menu.xml:

<item android:id="@+id/itemSearch"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@mipmap/ic_search_white_48dp"
    android:title="@string/action_search"
    app:showAsAction="ifRoom" />

i need your help guys!

Beyond
  • 3
  • 6
  • Change this line in item attribute : app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView" – Rajan1404930 Nov 15 '16 at 17:48
  • @Rajan1404930 i try it then i had a new logcat: – Beyond Nov 15 '16 at 17:58
  • @Rajan1404930 `java.lang.ClassCastException: android.widget.SearchView cannot be cast to android.support.v7.widget.SearchView at com.aeu.mlibrary.mlibraryaeu.SearchFragment.onCreateOptionsMenu(SearchFragment.java:76)` – Beyond Nov 15 '16 at 17:59

1 Answers1

0
SearchView searchView = (SearchView) menu.findItem(R.id.itemSearch);

This itself would get you the searchView object. I think the getActionView() is the reason for null-pointer exception.

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • im sorry @SoulRayder , i just edited my code, can you check again what's wrong with my code ? thanks – Beyond Nov 15 '16 at 17:46