Android SearchView

Asked 2 years ago, Updated 2 years ago, 65 views

In the fragment, searchView is implemented in the action bar. It used to work well public class Fragment1 extends android.support.v4.app.Fragment {}

There is an error after extends to v4 because of other tasks The searchView widget I used is Android.supprot.v7.widget.

MenuItem item = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) item.getActionView();

item.getActionView reports null interception error.

java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference at com.example.user.seoulapp.Fragment1.onCreateOptionsMenu(Fragment1.java:132)

What's the solution?

searchview android

2022-09-22 21:25

2 Answers

It's rather a simple part, so I think I missed something. I added the code I tested. I think it's better to compare the codes and look at them again

search_menu.xml

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />
</menu>

SearchViewFragment.java

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_search_view, container, false);
    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolBar);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    setHasOptionsMenu(true);
    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.search_menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }
        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });
}

Results

search, when clicked


2022-09-22 21:25

Please check if app:actionViewClass is set in the menu xml. NullPointerException may occur if the prefix is android:actionViewClass. Please refer to the code below and correct it.

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="@string/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />
</menu>


2022-09-22 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.