如何将活动实时数据中加载的对象传递给片段

时间:2019-05-02 15:41:07

标签: android android-fragments android-viewpager

我有3个片段,这3个片段都在一个Activity中,在我的Activity中,我通过实时数据和如下的viewmodel从firebase请求我的数据:

在我的活动的OnCreate中

mGameViewModel.getGame(release.getGame().getId()).observe(this, new Observer<Game>() {
                    @Override
                    public void onChanged(@Nullable Game game) {
                        if (game != null) {

                            mGameSectionsPagerAdapter.setGame(game);

                            // Load developers and publishers

                            List<String> developers = game.getDevelopersNames();
                            if (!developers.isEmpty()) {
                                String title = "Developer";
                                if (developers.size() > 1) {
                                    title = "Developers";
                                }
                                String message = title + ": " + TextUtils.join(", ", developers);
                                mDeveloperTxt.setText(message);
                            } else {
                                mDeveloperTxt.setVisibility(View.INVISIBLE);
                            }

                            List<String> publishers = game.getPublishersNames();
                            if (!publishers.isEmpty()) {
                                String title = "Publisher";
                                if (publishers.size() > 1) {
                                    title = "Publishers";
                                }
                                String message = title + ": " + TextUtils.join(", ", publishers);
                                mPublisherTxt.setText(message);
                            } else {
                                mPublisherTxt.setVisibility(View.INVISIBLE);
                            }
                        } else {
                            Toast.makeText(GamePageActivity.this, "not found", Toast.LENGTH_SHORT).show();
                        }
                    }
                }); 

我想将游戏对象传递给片段,因为这些片段以自己的方式使用游戏对象,这是我的ViewPagerAdapter代码:

public class GamePageSectionsPageAdapter extends FragmentStatePagerAdapter {
public final Fragment[] fragments;
public final String[] fragmentsTitle;

public GamePageSectionsPageAdapter(FragmentManager fm) {
    super(fm);
    fragments = new Fragment[3];
    fragmentsTitle = new String[3];
}

@Override
public Fragment getItem(int position) {
    Fragment fragment;
    switch (position) {
        case 0:
            fragment = new GameInfoFragment();
            fragments[position] = fragment;
            fragmentsTitle[position] = "General";
            break;
        case 1:
            fragment = new GameImagesFragment();
            fragments[position] = fragment;
            fragmentsTitle[position] = "Screenshots";
            break;
        case 2:
            fragment = new GameVideosFragment();
            fragments[position] = fragment;
            fragmentsTitle[position] = "Videos";
            break;
    }
    return fragments[position];
}

@Override
public int getCount() {
    return fragments.length;
}

public Fragment getFragment(final int position) {
    return fragments[position];
}

@Override
public CharSequence getPageTitle(int position) {
    return fragmentsTitle[position];
}

// Pass Game object to all three fragments
public void setGame(Game game) {
    ((GameInfoFragment) fragments[0]).onDataLoaded(game);
    ((GameImagesFragment) fragments[1]).onDataLoaded(game);
    ((GameVideosFragment) fragments[2]).onDataLoaded(game);
}
}

我将我的Game对象传递给mGameSectionsPagerAdapter.setGame(game);以便将数据加载到片段中,但是现在当我从本活动中退出应用程序,并且一段时间后(玩游戏和观看视频)从后台回到我的应用程序时,该应用程序崩溃并显示以下堆栈信息:

GameInfoFragment.onDataLoaded(com.testyapp.model.Game)' on a null object reference
        at com.testyapp.adapters.GamePageSectionsPageAdapter.setGame(GamePageSectionsPageAdapter.java:62)

片段为空,为什么?

0 个答案:

没有答案