简体   繁体   English

找不到类'android.support.v7.widget.SearchView $ 5'

[英]Could not find class 'android.support.v7.widget.SearchView$5'

I get this error in y Logcat. 我在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

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. 我正在使用v7 compat库以便在Android 2上安装ActionBar我正在实现搜索界面的东西。

Basic Setup Code (in onCreateOptionsMenu() ) 基本设置代码(在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. 不幸的是,这里的问题是我们正在调用仅在v14中支持的方法,因此当它尝试加载一些隐式使用的类时,我们会遇到“奇怪的”运行时错误。 That's not a very good explanation, but basically it's the same reason we need to use getSupportActionBar() instead of getActionBar() . 这不是一个很好的解释,但基本上我们需要使用getSupportActionBar()而不是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;
    }
});

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'. 您需要确保在Eclipse中正确添加Android支持库以从日志中删除以下错误'无法找到类android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11引用的类android.support.v7.widget.SearchView $ 5 ”。

The key thing to remember, don't forget to uncheck Android Dependencies when adding the Support Library because the v7 appcompat library has resources. 要记住的关键是,在添加支持库时不要忘记取消选中Android Dependencies ,因为v7 appcompat库有资源。 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. 有关如何使用资源添加支持库的信息,请参阅使用官方Google doco的资源添加库中的完整过程。

Excerpt from above referenced doco in case link changes in future: 摘录以上引用的doco以防万一将来链接发生变化:

  1. Make sure you have downloaded the Android Support Library using the SDK Manager. 确保您已使用SDK Manager下载了Android支持库。
  2. Create a library project and ensure the required JAR files are included in the project's build path: 创建一个库项目并确保所需的JAR文件包含在项目的构建路径中:
    • Select File > Import. 选择文件>导入。
    • Select Existing Android Code Into Workspace and click Next. 选择Existing Android Code Into Workspace,然后单击Next。
    • Browse to the SDK installation directory and then to the Support Library folder. 浏览到SDK安装目录,然后浏览到Support Library文件夹。 For example, if you are adding the appcompat project, browse to /extras/android/support/v7/appcompat/. 例如,如果要添加appcompat项目,请浏览到/ 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. 对于v7 appcompat项目,您现在应该看到一个名为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. 在新库项目中,展开libs /文件夹,右键单击每个.jar文件,然后选择“构建路径”>“添加到构建路径”。 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. 例如,在创建v7 appcompat项目时,将android-support-v4.jar和android-support-v7-appcompat.jar文件添加到构建路径。
    • 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. 在“订购和导出”选项卡中,检查刚刚添加到构建路径的.jar文件,以便它们可供依赖于此库项目的项目使用。 For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files. 例如,appcompat项目要求您导出android-support-v4.jar和android-support-v7-appcompat.jar文件。
    • Uncheck Android Dependencies. 取消选中Android依赖项。
    • Click OK to complete the changes. 单击“确定”完成更改。

You need to add missing Support Library to the Project. 您需要将缺少的支持库添加到项目中。 In Eclipse: Right click at Project 在Eclipse中:右键单击Project

Select Android Tools/Add Support Library... 选择Android工具/添加支持库...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法实例化类:android.support.v7.widget.SearchView - Cannot instantiate class: android.support.v7.widget.SearchView android.support.v7.widget.SearchView setQueryHint? - android.support.v7.widget.SearchView setQueryHint? 无法获取android.support.v7.widget.SearchView接受文本 - Cannot get android.support.v7.widget.SearchView to accept text android.support.v7.widget.SearchView对我不起作用 - android.support.v7.widget.SearchView wont work for me 使用android.support.v7.widget.SearchView时获取类转换异常 - Getting class cast exception while using android.support.v7.widget.SearchView 找不到从方法android.support.v4.widget.DrawerLayout引用的类'android.support.v4.widget.DrawerLayout $ 1' - Could not find class 'android.support.v4.widget.DrawerLayout$1', referenced from method android.support.v4.widget.DrawerLayout 没有找到类“android.support.v7.widget.CardView” - Didn't find class “android.support.v7.widget.CardView” “无法找到类'android.support.v4.app.FragmentActivity'”错误 - “Could not find class 'android.support.v4.app.FragmentActivity'” Error 错误无法实例化以下类:- android.support.v7.widget.CardView(打开类,显示错误日志) - Error The following classes could not be instantiated: - android.support.v7.widget.CardView (Open Class, Show Error Log) 从v4升级项目后找不到类'android.support.v7.app.NotificationCompat $ Builder' - Could not find class 'android.support.v7.app.NotificationCompat$Builder', after upgrading the project from v4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM