当我尝试像在文档中那样配置 SearchView 时,如果使用 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 上使用它。