我有一个ViewPager的主要活动。我正尝试使用适配器填充ViewPager。Android片段onCreateView尽管初始化为null ArrayList尽管初始化
这是我的片段类。
public class ClassListFragment extends Fragment {
private List<item_timetable_class> _classList;
public ClassListFragment() {
}
public ClassListFragment SetClassList (List<item_timetable_class> classList) {
ClassListFragment fragment = new ClassListFragment();
fragment._classList = classList;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// some code here
//In this line I am getting _classList = Null
class_card_adapter adapter = new class_card_adapter(_classList, getContext());
// some code here
}
}
这就是我所说的片段。请注意,在创建一个新的ClassListFragment对象之后,我调用SetClassList,以便内部变量_classList被设置。
// Set up the ViewPager with the sections adapter.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
for (int i = 0; i < weekday_list.size(); i++) {
ClassListFragment newFragment = new ClassListFragment();
newFragment.SetClassList(classCardList);
mSectionsPagerAdapter.addFragment(newFragment, weekday_list.get(i));
i++;
}
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
不幸的是,在主活动中正在执行下面一行时正在执行onCreateView方法。
mViewPager.setAdapter(mSectionsPagerAdapter);
而在那个时候,List _classList是空的。
可能是什么问题以及如何解决该问题?
你的答案是更多的信息!谢谢,但R.Zagórski的回答涉及最少量的代码更改。谢谢,你们都很棒! – BuggyCoder