Java 找不到类“android.support.v7.widget.SearchView$5”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18386739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find class 'android.support.v7.widget.SearchView$5'
提问by Bombolo
I get this error in y Logcat. Does anyone know what it is?
我在 y Logcat 中收到此错误。有谁知道它是什么?
08-22 19:02:57.830: E/dalvikvm(660): Could not find class 'android.support.v7.widget.SearchView', referenced from method android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11
回答by xbakesx
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()
)
基本设置代码(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()
.
不幸的是,这里的问题是我们正在调用一个仅在 v14 中支持的方法,因此当它尝试加载一些隐式使用的类时,我们会得到一个“奇怪的”运行时错误。这不是一个很好的解释,但基本上这与我们需要使用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;
}
});
回答by DK2
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 Dependencieswhen 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.
要记住的关键是,在添加 Support Library 时不要忘记取消选中 Android Dependencies,因为 v7 appcompat 库有资源。对支持库项目中的依赖项进行更改后,清理支持库项目,就是这样。
Refer to complete procedure in section Adding Libraries with Resourcesof official Google doco on how to add support libraries with resources.
有关如何使用资源添加支持库,请参阅Google 官方文档的使用资源添加库部分中的完整过程。
Excerpt from above referenced doco in case link changes in future:
上面引用的 doco 的摘录,以防将来链接发生变化:
- Make sure you have downloaded the Android Support Library using the SDK Manager.
- 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.
- 确保您已使用 SDK 管理器下载了 Android 支持库。
- 创建一个库项目并确保所需的 JAR 文件包含在项目的构建路径中:
- 选择文件 > 导入。
- 选择 Existing Android Code Into Workspace 并单击 Next。
- 浏览到 SDK 安装目录,然后浏览到 Support Library 文件夹。例如,如果您要添加 appcompat 项目,请浏览至 /extras/android/support/v7/appcompat/。
- 单击完成以导入项目。对于 v7 appcompat 项目,您现在应该会看到一个名为 android-support-v7-appcompat 的新项目。
- 在新的库项目中,展开 libs/ 文件夹,右键单击每个 .jar 文件并选择 Build Path > Add to Build Path。例如,在创建 v7 appcompat 项目时,将 android-support-v4.jar 和 android-support-v7-appcompat.jar 文件添加到构建路径。
- 右键单击项目并选择构建路径 > 配置构建路径。在 Order and Export 选项卡中,检查您刚刚添加到构建路径的 .jar 文件,以便它们可用于依赖于该库项目的项目。例如,appcompat 项目要求您导出 android-support-v4.jar 和 android-support-v7-appcompat.jar 文件。
- 取消选中 Android 依赖项。
- 单击确定以完成更改。
回答by MKK
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 工具/添加支持库...