简体   繁体   English

尽管初始化,Android Fragment onCreateView的ArrayList也为空

[英]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) (执行所需操作的正确方法是将BundleParcelable对象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.

相关问题 Android-片段视图在onCreateView之外获取NULL - Android - Fragment Views get NULL outside onCreateView onCreateView中的Android片段getActivity()。getApplicationContext()为null - Android Fragment getActivity().getApplicationContext() in onCreateView is null Android 片段 onCreateView 延迟 - Android fragment onCreateView delayed Android:访问内部AsyncTask类的变量,但在片段的onCreateView中将其获取为null - Android: Accessing a variable of an inner AsyncTask class but getting it as null in onCreateView of a fragment 带有错误片段的ViewPagers oncreateView的Android片段 - Android Fragment with ViewPagers oncreateView Called in Wrong Fragment Android片段oncreateview被调用,但视图未膨胀且没有错误 - Android fragment oncreateview is called but view is not inflated and there are no errors 使用onCreateView的Android片段但无法添加Firebase - android fragment using onCreateView but cant add firebase 在onCreateView()外列出适配器null-Android - List adapter null outside onCreateView() - android Android Fragment 初始化 - Android Fragment initialization Android Java:片段在onCreateView()中返回空视图 - Android Java: Fragments returning null view in onCreateView()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM