2

I can't cast my searchview with library actionbarsherlock! log:

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

The searchview it's not inside the actionbar. On the onCreate i wrote:

// Searchview
    mSearchView = (SearchView) findViewById(R.id.search_view); 

    lvItem.setAdapter(new ArrayAdapter<String>(this,
             R.layout.customlistview,
             mStrings));
    lvItem.setTextFilterEnabled(true);
     setupSearchView();

   ////

Where mStrings is private final ArrayList<String> mStrings = MainActivity.itemArrey; and itemArrey are my datas in the listview. Why it crashes?

EDIT: the import is: import com.actionbarsherlock.widget.SearchView; so it's strange

4

1 回答 1

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);
于 2013-12-04T08:48:52.323 回答