Home » Android » java – Could not find class 'android.support.v7.widget.SearchView$5'

java – Could not find class 'android.support.v7.widget.SearchView$5'

Posted by: admin June 15, 2020 Leave a comment

Questions:

I get this error in y Logcat.
Does anyone know what it is?

    08-22 19:02:57.830: E/dalvikvm(660): Could not find class 'android.support.v7.widget.SearchView$5', referenced from method android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11
How to&Answers:

There isn’t a lot of code to go off of here, but I ran into this situation myself and here is what happened to me:

I was using the v7 compat library in order to have an ActionBar on Android 2
I am implementing the search interface stuff.

Basic Setup Code (in onCreateOptionsMenu())

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

Bad Code

searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        // on search expand stuff
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        // on search collapse stuff
        return true;
    }
});

Unfortunately the problem here is that we are calling a method that is only supported in v14 so we get a “weird” run-time error when it tries to load some classes that are implicitly used. That’s not a very good explanation, but basically it’s the same reason we need to use getSupportActionBar() instead of getActionBar().

Good Code

searchMenuItem.setSupportOnActionExpandListener(new MenuItemCompat.OnActionExpandListener() {
    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        // do work
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        // do work
        return true;
    }
});

Answer:

You need to ensure you add the Android Support Library correctly in Eclipse to remove the following error from the log ‘Could not find class android.support.v7.widget.SearchView$5 referenced from method android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11’.

The key thing to remember, don’t forget to uncheck Android Dependencies when adding the Support Library because the v7 appcompat library has resources. After making the change to your dependencies in your support library project, clean the support library project and that’s it.

Refer to complete procedure in section Adding Libraries with Resources of official Google doco on how to add support libraries with resources.

Excerpt from above referenced doco in case link changes in future:

  1. Make sure you have downloaded the Android Support Library using the
    SDK Manager.
  2. Create a library project and ensure the required JAR
    files are included in the project’s build path:

    • Select File > Import.
    • Select Existing Android Code Into Workspace and click Next.
    • Browse to the SDK installation directory and then to the Support
      Library folder. For example, if you are adding the appcompat
      project, browse to /extras/android/support/v7/appcompat/.
    • Click Finish to import the project. For the v7 appcompat project, you
      should now see a new project titled android-support-v7-appcompat.
    • In the new library project, expand the libs/ folder, right-click each
      .jar file and select Build Path > Add to Build Path. For example,
      when creating the the v7 appcompat project, add both the
      android-support-v4.jar and android-support-v7-appcompat.jar files to
      the build path.
    • Right-click the project and select Build Path > Configure Build Path. In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
    • Uncheck Android Dependencies.
    • Click OK to complete the changes.

Answer:

You need to add missing Support Library to the Project.
In Eclipse: Right click at Project

Select Android Tools/Add Support Library…