Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarSherlock SearchView cast error

When I try to configure SearchView like in documentation I get this error if using ActionBarSherlock:

FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.SearchView cannot be cast to com.actionbarsherlock.widget.SearchView
    at MY_PACKAGE.ACTIVITY.onCreateOptionsMenu(MenuDrawerBase.java:181)
    at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:45)
    at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
    at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:60)
    at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:165)
    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393)
    at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2913)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

My code is exactly as in example:

import com.actionbarsherlock.view.*;
import com.actionbarsherlock.widget.SearchView;
......
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);
        searchView.setSubmitButtonEnabled(true);
        return true;
    }

Didn't find description of this nowhere so asking you: what's this?
By the way, if I use import android.widget.SearchView; instead of import com.actionbarsherlock.widget.SearchView; the application compiles and works with API greater than 11 but I need it on version 10.

like image 298
gorodechnyj Avatar asked Nov 07 '12 10:11

gorodechnyj


1 Answers

My apologies guys, but this was my mistake.
Be aware of what class are you referencing in menu xml resource. In my case it was:

<item android:id="@+id/action_search"
    android:title="@string/action_search"
    android:icon="@drawable/action_search_light"
    android:showAsAction="always|collapseActionView"
    android:actionViewClass="android.widget.SearchView"/>

and this caused cast error. Changed actionViewClass to com.actionbarsherlock.widget.SearchView and got exactly what was expecting!

like image 138
gorodechnyj Avatar answered Nov 15 '22 06:11

gorodechnyj