我无法使用库actionbarsherlock投射我的searchview! 日志:
10-14 16:47:10.957: E/AndroidRuntime(17871): FATAL EXCEPTION: main
10-14 16:47:10.957: E/AndroidRuntime(17871): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dd.virtualregister/com.dd.virtualregister.MainActivity}: java.lang.ClassCastException: android.widget.SearchView cannot be cast to com.actionbarsherlock.widget.SearchView
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2409)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.app.ActivityThread.access$600(ActivityThread.java:158)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.os.Looper.loop(Looper.java:137)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.app.ActivityThread.main(ActivityThread.java:5323)
10-14 16:47:10.957: E/AndroidRuntime(17871): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 16:47:10.957: E/AndroidRuntime(17871): at java.lang.reflect.Method.invoke(Method.java:525)
10-14 16:47:10.957: E/AndroidRuntime(17871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:743)
10-14 16:47:10.957: E/AndroidRuntime(17871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
10-14 16:47:10.957: E/AndroidRuntime(17871): at dalvik.system.NativeStart.main(Native Method)
10-14 16:47:10.957: E/AndroidRuntime(17871): Caused by: java.lang.ClassCastException: android.widget.SearchView cannot be cast to com.actionbarsherlock.widget.SearchView
10-14 16:47:10.957: E/AndroidRuntime(17871): at com.dd.virtualregister.MainActivity.onCreate(MainActivity.java:101)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.app.Activity.performCreate(Activity.java:5248)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-14 16:47:10.957: E/AndroidRuntime(17871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2313)
10-14 16:47:10.957: E/AndroidRuntime(17871): ... 11 more
搜索视图不在操作栏内。在onCreate上我写道:
// Searchview
mSearchView = (SearchView) findViewById(R.id.search_view);
lvItem.setAdapter(new ArrayAdapter<String>(this,
R.layout.customlistview,
mStrings));
lvItem.setTextFilterEnabled(true);
setupSearchView();
////
其中mStrings为private final ArrayList<String> mStrings = MainActivity.itemArrey;
,而itemArrey是listview中的数据。
为什么会崩溃?
编辑:导入是:import com.actionbarsherlock.widget.SearchView;
所以很奇怪
答案 0 :(得分:1)
直接使用findViewById无法找到菜单项
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu_xml, menu);
searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
//do your stuffs
return super.onCreateOptionsMenu(menu);
}
如果您要定位较低版本,请使用
SearchView searchView =(SearchView) MenuItemCompat.getActionView(searchItem);