[英]Android Fragment onCreateView has null ArrayList despite initialization
I have a main activity with a ViewPager. 我有一个ViewPager的主要活动。 I am trying to populate the ViewPager using an Adapter.
我正在尝试使用适配器填充ViewPager。
This is my Fragment Class. 这是我的片段类。
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
}
}
This is how I am calling the Fragment. 这就是我所说的片段。 Please note that Just after creating a new ClassListFragment Object, I am calling SetClassList so that the internal variable _classList is Set.
请注意,就在创建新的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);
Unfortunately, the onCreateView method is being executed when the below line is being executed in the main activity. 不幸的是,当在主活动中执行以下行时,将执行onCreateView方法。
mViewPager.setAdapter(mSectionsPagerAdapter);
And at that time, the List _classList is coming as null. 那时,List _classList为null。
What could be the problem and How can I resolve the issue? 可能是什么问题,我该如何解决?
You are creating ClassListFragment
: 您正在创建
ClassListFragment
:
ClassListFragment newFragment = new ClassListFragment();
And then do not keep reference to fragment returned from SetClassList
: 然后不要保留对从
SetClassList
返回的片段的引用:
newFragment.SetClassList(classCardList);
Modify SetClassList
: 修改
SetClassList
:
public ClassListFragment SetClassList (List<item_timetable_class> classLis)
{
_classList = classList;
return this;
}
And keep reference to the returned Fragment
: 并保留对返回的
Fragment
引用:
newFragment = newFragment.SetClassList(classCardList);
Please refer: Best practice for instantiating a new Android Fragment 请参考: 实例化新Android片段的最佳做法
(The correct way to do what you want is using Bundle
and Parcelable
objects into the Fragment arguments) (执行所需操作的正确方法是将
Bundle
和Parcelable
对象Parcelable
Fragment参数)
Anyway, you called two separate "constructors" here. 无论如何,您在这里调用了两个单独的“构造函数”。
ClassListFragment newFragment = new ClassListFragment();
newFragment.SetClassList(classCardList);
public ClassListFragment SetClassList
returns a new ClassListFragment
, it does not set the current one. public ClassListFragment SetClassList
返回一个new ClassListFragment
,它不会设置当前的new ClassListFragment
。
Try this instead. 试试这个吧。
public static ClassListFragment newInstance(List<item_timetable_class> classList) {
ClassListFragment fragment = new ClassListFragment();
fragment._classList = classList;
return fragment;
}
And 和
for (int i = 0; i < weekday_list.size(); i++) {
mSectionsPagerAdapter.addFragment(
ClassListFragment.newInstance(classCardList),
weekday_list.get(i));
// i++; // You already increment this in the loop...
}
What I would do is this: 我要做的是:
public static ClassListFragment newInstance(final List<item_time_table_class> classList) {
ClassListFragment fragment = new ClassListFragment();
Bundle args = new Bundle()
args.putParcelable(key, classList);
fragment.setArguments(args);
return fragment;
}
ClassListFragment::onCreate() {
classList = getArguments().getParcelable(key);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.