[英]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以防万一将来链接发生变化:
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.