尽管初始化,Android Fragment onCreateView仍具有null ArrayList

时间:2017-02-27 20:18:04

标签: java android android-fragments

我有一个ViewPager的主要活动。我正在尝试使用适配器填充ViewPager。

这是我的片段类。

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。

    // 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将变为null。

可能是什么问题,如何解决问题?

3 个答案:

答案 0 :(得分:0)

您正在创建ClassListFragment

ClassListFragment newFragment = new ClassListFragment();

然后不要继续引用从SetClassList返回的片段:

newFragment.SetClassList(classCardList);

修改SetClassList

public ClassListFragment SetClassList (List<item_timetable_class> classLis)
{
    _classList = classList;
    return this;
}

并继续引用返回的Fragment

newFragment = newFragment.SetClassList(classCardList);

答案 1 :(得分:0)

请参阅:Best practice for instantiating a new Android Fragment

(执行所需操作的正确方法是在Fragment参数中使用BundleParcelable个对象)

无论如何,你在这里叫了两个独立的“构造函数”。

ClassListFragment newFragment = new ClassListFragment();
newFragment.SetClassList(classCardList);

public ClassListFragment SetClassList 返回 a new ClassListFragment,它不会设置当前的public static ClassListFragment newInstance(List<item_timetable_class> classList) { ClassListFragment fragment = new ClassListFragment(); fragment._classList = classList; return fragment; }

试试这个。

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...
}

并且

SELECT JobNo
      ,Prepared1
      ,Prepared2
  FROM [test]
SELECT 
  COUNT(CASE WHEN Prepared1 >1  THEN 1 END) +
  COUNT(CASE WHEN Prepared2 >1 THEN 1 END) as 'Done'
  FROM [test]
GROUP BY JobNo

答案 2 :(得分:0)

我要做的是:

    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);
}