【发布时间】:2014-04-24 17:01:06
【问题描述】:
我有以下 android 代码。当我向前浏览选项卡时它工作正常,但是一旦我尝试向后滑动应用程序就会崩溃。 问题出在“rootView = (ScrollView) gTabView.get(t);”线上当它尝试从全局变量第二次分配视图时。 我怎样才能避免这种情况? 谢谢
public class DocPageActivity extends FragmentActivity {
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public static ActionBar actionBar = null;
public static int gtab=0;
public static ArrayList gTabView = new ArrayList();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//InstState = savedInstanceState;
super.onCreate(savedInstanceState);
setContentView(R.layout.pager_obj);
gTabView.clear();
gtab = 0;
BuildTabsContents();//creates view to dislay in each tab in gTabView,store countNo of tabs in gtab
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
// Set up action bar.
final ActionBar actionBar = getActionBar();
// Specify that the Home button should show an "Up" caret, indicating that touching the
// button will take the user one step up in the application's hierarchy.
actionBar.setDisplayHomeAsUpEnabled(true);
// Set up the ViewPager, attaching the adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
/**
* A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a fragment
* representing an object in the collection.
*/
public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_OBJECT, i);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return gtab;
}
@Override
public CharSequence getPageTitle(int position) {
return "TAB " + (position + 1);
}
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
Bundle args = getArguments();
int t = args.getInt(ARG_OBJECT);
rootView = (ScrollView) gTabView.get(t);
return rootView;
}
}
}
【问题讨论】:
标签: android tabs android-actionbar fragment