Search-view not working correctly in android toolb

2019-09-16 08:18发布

问题:

I am using search view in my Flickr app but when I run, it just display the title of search view not it's search icon and when I click on the title it should display and area to type the search query but nothing happen it treat like a simple toolbar item

this is menu xml file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_item_search"
        android:title="@string/search"
        app:actionViewClass="android:support.v7.widget.searchView"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/menu_item_clear"
          android:title="@string/clear_search"
          app:showAsAction="never"/>
</menu>

and this is a picture of app in emulator

and when I refer to code and set a text listener for search view action view class it refer a null reference error

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

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

        MenuItem searchItem = menu.findItem(R.id.menu_item_search);

        final SearchView searchView = (SearchView) searchItem.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {

                Log.d(TAG,"QueryTextSubmit: "+s);
                updateItems();
                return true;

            }

            @Override
            public boolean onQueryTextChange(String s) {

                Log.d(TAG,"queryTextChanged: "+s);
                return true;
            }
        });


    }

and this is my logcat after crashing

09-05 15:04:25.759 14339-14339/com.bignerdranch.android.photogallery E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.bignerdranch.android.photogallery, PID: 14339
                                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference
                                                                                           at com.bignerdranch.android.photogallery.PhotoGalleryFragment.onCreateOptionsMenu(PhotoGalleryFragment.java:71)
                                                                                           at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2291)
                                                                                           at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:3011)
                                                                                           at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:328)
                                                                                           at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:363)
                                                                                           at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
                                                                                           at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1368)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1648)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:137)
                                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)

回答1:

As Commented You need to write android:support.v7.widget.SearchView to address the appropriate class naming convention

and also add collapseActionView| for collapsing feature

<item
        android:id="@+id/menu_item_search"
        android:title="@string/search"
        app:actionViewClass="android:support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom"/>


回答2:

I have changed the version of appcompact library to 25.3.0 and it solved

I think the library was corrupted.

this is the code of my gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.0'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:recyclerview-v7:25.3.0'
}


回答3:

change this use android:support.v7.widget.SearchView instedof android:support.v7.widget.searchView

<item
   android:id="@+id/menu_item_search"
   android:title="@string/search"
   app:actionViewClass="android:support.v7.widget.SearchView"
   app:showAsAction="ifRoom"/>