2013-05-30 65 views
0

嘗試使用ViewPager + Fragments + ActionBarSherlock使用Menu-Items時,出現以下錯誤。我幾乎肯定這是一個簡單的錯誤,我只是不能弄清楚如何解決。ABS和ViewPager

的logcat:

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); 
} 
+0

Watson.java中的第118行是什麼 –

回答

1

mFragments顯然是ArrayList。您正試圖將其轉換爲ViewPager

+0

感謝您的回覆,是的,看看我試圖做的是讓我的菜單項控制我的viewpager中正在查看的任何片段。我已經發布了一個與菜單項和我的viewpager和fragment有關的問題。介意看看這個問題,它有我的主要來源,並且我確定你會看到我想要做的事情。 http://stackoverflow.com/questions/16844288/how-to-use-actionbar-items-with-viewpager-fragments –