尝试将菜单项与 ViewPager + Fragments + ActionBarSherlock 一起使用时出现以下错误。我几乎可以肯定这是一个简单的错误,我似乎无法弄清楚如何解决。
日志猫:
05-30 17:11:28.237: E/AndroidRuntime(19406): FATAL EXCEPTION: main
05-30 17:11:28.237: E/AndroidRuntime(19406): java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.support.v4.view.ViewPager
05-30 17:11:28.237: E/AndroidRuntime(19406): at org.greenbot.technologies.solarphase.MainActivity.onOptionsItemSelected(MainActivity.java:74)
05-30 17:11:28.237: E/AndroidRuntime(19406): at android.support.v4.app.Watson.onMenuItemSelected(Watson.java:118)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchOptionsItemSelected(ActionBarSherlockNative.java:93)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:205)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1033)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
05-30 17:11:28.237: E/AndroidRuntime(19406): at android.view.View.performClick(View.java:4211)
05-30 17:11:28.237: E/AndroidRuntime(19406): at android.view.View$PerformClick.run(View.java:17362)
05-30 17:11:28.237: E/AndroidRuntime(19406): at android.os.Handler.handleCallback(Handler.java:725)
05-30 17:11:28.237: E/AndroidRuntime(19406): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 17:11:28.237: E/AndroidRuntime(19406): at android.os.Looper.loop(Looper.java:137)
05-30 17:11:28.237: E/AndroidRuntime(19406): at android.app.ActivityThread.main(ActivityThread.java:5226)
05-30 17:11:28.237: E/AndroidRuntime(19406): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 17:11:28.237: E/AndroidRuntime(19406): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-30 17:11:28.237: E/AndroidRuntime(19406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-30 17:11:28.237: E/AndroidRuntime(19406): at dalvik.system.NativeStart.main(Native Method)
当我在下面的代码中单击我的菜单项时,就会发生这种情况。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater Inflater = getSupportMenuInflater();
Inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem Item) {
final int current = ((ViewPager) mFragments).getCurrentItem();
WebView web = (WebView) findViewById(current);
switch (Item.getItemId()) {
case R.id.menu_back:
web.goBack();
return true;
case R.id.menu_forward:
web.goForward();
return true;
case R.id.menu_refresh:
web.reload();
return true;
}
return super.onOptionsItemSelected(Item);
}