ActionBarSherlock SearchView强制转换错误

时间:2012-11-07 10:03:55

标签: android actionbarsherlock searchview

当我尝试configure SearchView like in documentation时,如果使用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)

我的代码与示例完全相同:

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;
    }

没有找到这方面的描述,所以问你:这是什么? 顺便说一句,如果我使用import android.widget.SearchView;而不是import com.actionbarsherlock.widget.SearchView;,应用程序将编译并使用大于11的API,但我需要在版本10上。

2 个答案:

答案 0 :(得分:26)

我道歉,但这是我的错误 请注意菜单xml资源中引用的类。就我而言,它是:

<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"/>

这导致了施法错误。将 actionViewClass 更改为 com.actionbarsherlock.widget.SearchView ,并获得了所期待的内容!

答案 1 :(得分:1)

option_menu.xml更改为

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
          android:title="@string/menu_search"
          android:icon="@drawable/ic_menu_search"
          android:showAsAction="collapseActionView|ifRoom"
          android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>