最近在使用holoeverywhere做某事,但现在遇到一个问题,当我到达最后一个标签后一直向右滑动时,程序会加载第一个标签的视图但没有更多的标签,所以程序会崩溃. 问题描述如下,我真的需要有人帮助我。谢谢大家!
以下是我的代码:
public static class TabFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getSupportActivity());
textView.setText("Position: " + getArguments().getInt("position"));
return textView;
}
}
private static Bundle make(int i) {
Bundle bundle = new Bundle();
bundle.putInt("position", i);
return bundle;
}
@Override
protected void onHandleTabs() {
getSupportActionBar().removeAllTabs();
for(int i = 0; i < 5; i++)
addTab("Tab #" + i, TabFragment.class, make(i + 1));
}
问题是当我在最后一个选项卡之后滑动时,视图将如下所示:
并且程序将崩溃并出现以下错误:
05-05 21:33:26.258: E/AndroidRuntime(1573): FATAL EXCEPTION: main
05-05 21:33:26.258: E/AndroidRuntime(1573): java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5
05-05 21:33:26.258: E/AndroidRuntime(1573): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
05-05 21:33:26.258: E/AndroidRuntime(1573): at java.util.ArrayList.get(ArrayList.java:304)
05-05 21:33:26.258: E/AndroidRuntime(1573): at com.android.internal.app.ActionBarImpl.getTabAt(ActionBarImpl.java:989)
05-05 21:33:26.258: E/AndroidRuntime(1573): at com.actionbarsherlock.internal.app.ActionBarWrapper.getTabAt(ActionBarWrapper.java:425)
05-05 21:33:26.258: E/AndroidRuntime(1573): at org.holoeverywhere.app.TabSwipeActivity.dispatchTabSelected(TabSwipeActivity.java:135)
05-05 21:33:26.258: E/AndroidRuntime(1573): at org.holoeverywhere.app.TabSwipeActivity.access$2(TabSwipeActivity.java:128)
05-05 21:33:26.258: E/AndroidRuntime(1573): at org.holoeverywhere.app.TabSwipeActivity$TabSwipeAdapter.onPageSelected(TabSwipeActivity.java:67)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:538)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1943)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.View.dispatchTouchEvent(View.java:5486)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1714)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
05-05 21:33:26.258: E/AndroidRuntime(1573): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1892)
05-05 21:33:26.258: E/AndroidRuntime(1573): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
05-05 21:33:26.258: E/AndroidRuntime(1573): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1840)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.View.dispatchPointerEvent(View.java:5662)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2863)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.os.Looper.loop(Looper.java:137)
05-05 21:33:26.258: E/AndroidRuntime(1573): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-05 21:33:26.258: E/AndroidRuntime(1573): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 21:33:26.258: E/AndroidRuntime(1573): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 21:33:26.258: E/AndroidRuntime(1573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-05 21:33:26.258: E/AndroidRuntime(1573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-05 21:33:26.258: E/AndroidRuntime(1573): at dalvik.system.NativeStart.main(Native Method)
我还使用了一些自定义设置:
getSupportActionBar().setCustomView(getLayoutInflater().inflate(R.layout.****,null));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
View homeIcon = findViewById(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.id.home : R.id.abs__home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
和这个:
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
以下是演示的代码,我只是不明白。在演示中,当我到达最后一个选项卡后,程序将在屏幕右侧显示如下内容:
这些是演示的代码:
public static class TabFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("DemoTabsActivity", "test" + getArguments().getInt("position"));
TextView textView = new TextView(getSupportActivity());
textView.setText("Position: " + getArguments().getInt("position"));
return textView;
}
}
private static Bundle make(int i) {
Bundle bundle = new Bundle();
bundle.putInt("position", i);
return bundle;
}
@Override
protected void onHandleTabs() {
getSupportActionBar().removeAllTabs();
for(int i = 0; i < 10; i++)
addTab("Tab #" + i, TabFragment.class, make(i + 1));
Log.d("TAG", "getSupportActionBar().getTabCount()" + getSupportActionBar().getTabCount());
}