Appcompatv7 search widget crashes app

874 Views Asked by At

I have implemented Appcompat V7 Search Widget successfully. while Debugging app on phone it shows no problem.(on many phones I tried) But when I export signed application. after installing and opening that activity crashes. Showing error in OnCreateOptionsMenu() method at Search Widget

here is menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/group_search_box"
android:title="@string/search_label"
android:icon="@drawable/ic_action_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />

And here is how it is implemented.

SearchManager searchManager = (SearchManager) 
getActivity().getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.group_search_box);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

This was working properly on all phones while debugging but after building apk it is not working.

I think this happens because of proguard. how can I fix this bug?

3

There are 3 best solutions below

0
On

In menu.xml file in search item just changed android:actionViewClass="android.support.v7.widget.SearchView" to app:actionViewClass="android.support.v7.widget.SearchView" and the code workes fine, now search item looks like this:

<item android:id="@+id/search_menu"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="@string/search_title"
    android:icon="@drawable/ic_search_white_24dp"
    app:showAsAction="ifRoom|collapseActionView" />
5
On

Try this:

res- menu-> actionbar_activity_base.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/item_menu_search"
       android:icon="@drawable/ic_action_search"
       yourapp:showAsAction="ifRoom|collapseActionView"
         yourapp:actionViewClass="android.support.v7.widget.SearchView" >
    </item>
</menu>

And the java:

import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;

private SearchView mSearchView;


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbar_activity_base, menu);
        final MenuItem searchItem = menu.findItem(R.id.item_menu_search);
        mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

        mSearchView.setQueryHint(Html
                .fromHtml("<font color='#ffffff'> Search All</font>"));
        // mSearchView.setHintTextColor(getResources().getColor(android.R.color.white));
        // / mSearchView.setTextSize(14);
        isClosed = true;
        mSearchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String searchString) {
                // TODO Auto-generated method stub
                Log.i("fb", searchString + "submit");

            }

            @Override
            public boolean onQueryTextChange(String arg0) {
                // TODO Auto-generated method stub
                Log.i("fb", arg0 + "change");
                return false;
            }
        });

        mSearchView
                .setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {

                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        // TODO Auto-generated method stub

                        if (!hasFocus) {

                            Log.i("fb", "focus chagne");
                            searchItem.collapseActionView();
                            mSearchView.setQuery("", false);



                        }
                    }
                });


        return true;
    }
0
On

You should have a proguard-rules.pro file in your module directory.

Add the following to it...

-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }