我使用tabsListener作为ActionBar标签来设置标签。在我的phoneUi中,一切都很“好”,但在我的tabletUi中,它会引发异常。
09-15 15:46:27.588: W/System.err(31772): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
09-15 15:46:27.799: W/System.err(31772): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
09-15 15:46:27.799: W/System.err(31772): at java.util.ArrayList.get(ArrayList.java:304)
09-15 15:46:27.799: W/System.err(31772): at com.android.internal.app.ActionBarImpl.getTabAt(ActionBarImpl.java:1168)
09-15 15:46:27.799: W/System.err(31772): at com.actionbarsherlock.internal.app.ActionBarWrapper.getTabAt(ActionBarWrapper.java:423)
09-15 15:46:27.799: W/System.err(31772): at de.arvidg.onlineradio.StartActivity.onPageSelected(StartActivity.java:1270)
09-15 15:46:27.799: W/System.err(31772): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:445)
09-15 15:46:27.799: W/System.err(31772): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:405)
09-15 15:46:27.799: W/System.err(31772): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:386)
09-15 15:46:27.799: W/System.err(31772): at de.arvidg.onlineradio.TabsListener.onTabSelected(TabsListener.java:27)
09-15 15:46:27.799: W/System.err(31772): at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:344)
09-15 15:46:27.799: W/System.err(31772): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
09-15 15:46:27.799: W/System.err(31772): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
09-15 15:46:27.799: W/System.err(31772): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
09-15 15:46:27.799: W/System.err(31772): at com.actionbarsherlock.internal.app.ActionBarWrapper.addTab(ActionBarWrapper.java:377)
09-15 15:46:27.799: W/System.err(31772): at de.arvidg.onlineradio.StartActivity.tabletUI(StartActivity.java:403)
09-15 15:46:27.799: W/System.err(31772): at de.arvidg.onlineradio.StartActivity.onCreate(StartActivity.java:310)
09-15 15:46:27.799: W/System.err(31772): at android.app.Activity.performCreate(Activity.java:5008)
09-15 15:46:27.799: W/System.err(31772): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-15 15:46:27.799: W/System.err(31772): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
09-15 15:46:27.799: W/System.err(31772): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2207)
09-15 15:46:27.799: W/System.err(31772): at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-15 15:46:27.799: W/System.err(31772): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
09-15 15:46:27.799: W/System.err(31772): at android.os.Handler.dispatchMessage(Handler.java:99)
09-15 15:46:27.799: W/System.err(31772): at android.os.Looper.loop(Looper.java:137)
09-15 15:46:27.799: W/System.err(31772): at android.app.ActivityThread.main(ActivityThread.java:4899)
09-15 15:46:27.799: W/System.err(31772): at java.lang.reflect.Method.invokeNative(Native Method)
09-15 15:46:27.799: W/System.err(31772): at java.lang.reflect.Method.invoke(Method.java:511)
09-15 15:46:27.799: W/System.err(31772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-15 15:46:27.799: W/System.err(31772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-15 15:46:27.799: W/System.err(31772): at dalvik.system.NativeStart.main(Native Method)
抛出异常的方法:
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("TAG", "onTabSelected tag = " + mTag);
try { mPager.setCurrentItem(Integer.parseInt(mTag)); }
catch(Exception e) { Log.e("TAG", "mPager.setCurrentItem Exception " + e); e.printStackTrace(); Log.d("TabListener", "Error onTabSelected = " + e); }
}
任何想法?需要更多代码,请问。
答案 0 :(得分:1)
你的片段大小是1,但你传递索引1.这意味着有两个Fragments
,一个在索引0,另一个在索引1.集合从0开始计数,所以你应该这样做:
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("TAG", "onTabSelected tag = " + mTag);
try { mPager.setCurrentItem(Integer.parseInt(mTag) - 1); } ///CHANGED IT TO -1
catch(Exception e) { Log.e("TAG", "mPager.setCurrentItem Exception " + e);
e.printStackTrace(); Log.d("TabListener", "Error onTabSelected = " + e); }
}