我的应用上有一个searchview。我可以在搜索框上输入,但是,如果我按搜索应用程序崩溃。它说
11-25 00:57:09.446: E/AndroidRuntime(1914): java.lang.ClassCastException: com.actionbarsherlock.widget.SearchView cannot be cast to android.widget.SearchView
但是,我在菜单文件中声明的内容是正确的。
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="Quezon City Guide"
android:icon="@drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>
这是我称之为
的方法public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.options_menu, (Menu) menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我不知道我做错了什么。
这是我的导入
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import com.actionbarsherlock.widget.SearchView;
import com.actionbarsherlock.app.SherlockActivity;
有人请求帮助。谢谢!
答案 0 :(得分:0)
好像您正在尝试将Android SearchView
的{{1}}映射到actionbar
的{{1}}。那是你的错误!
答案 1 :(得分:0)
private SearchView mSearchView = null;
进入OnCreateOptionMenu使用以下代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
return true;
}
private void setupSearchView(MenuItem searchItem) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// mSearchView.setSearchableInfo(info);
}
mSearchView.setOnQueryTextListener(this);
}
答案 2 :(得分:0)
尝试在menu.xml文件中使用android.widget.SearchView
答案 3 :(得分:0)
您很可能在导入语句中引用默认的Android Widget而不是Sherlock Search Widget:
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();