SearchView using AppCompat

后端 未结 2 899
天命终不由人
天命终不由人 2021-02-02 13:00

I implemented SearchView in Actionbar before using appcompat.v7 but when I want to use the SearchView with support library v7 it shows null exception

In style

         


        
相关标签:
2条回答
  • 2021-02-02 13:17

    If you change android:actionViewClass to app:actionViewClass your existing code will continue working.

    0 讨论(0)
  • 2021-02-02 13:38

    You should use the static methods in MenuItemCompat do deal with all AppCompat menu items. This was mentioned in this blog post as the last item under 'New Integration'. Just replace your SearchView declaration with the following.

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

    Here's a link to the MenuItemCompat documentation.

    Edit: I just assumed you are using the latest version of AppCompat with the support for the new Toolbar widget.

    0 讨论(0)
提交回复
热议问题