我正在使用带有FragmentActivity的FragmentPagerAdapter来创建可滑动的 fragment 。

我在该pagerAdapter中的第一个 fragment 如下所示。

public class MySummaryFragment extends CommonFragment implements OnPageChangeListener
{
   private Context mContext;
   private View mMyView;

LinearLayout mDetailLayout;
TextView mDateTxtView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    System.out.println("onCreateView() Market summary");
    mMyView = inflater.inflate(R.layout.my_page_summary_fragment_layout, container,false);
    mDetailLayout = (LinearLayout)mMyView.findViewById(R.id.SummaryDetailLayout);
    mDateTxtView = (TextView) mMyView.findViewById(R.id.txtDate);

    Account_Id = AccountDetails.getInstance(mContext).getClientCodes().get(0);
    sendRequest(Account_Id);

    return mMyView;

}


 @Override
public void onPageSelected(int arg0) {
    System.out.println("inside onPageScrollStateChanged()");
    System.out.println("mMyView is null ? = "+(mMyView==null));
    mDateTxtView = (TextView) mMyView.findViewById(R.id.txtMarketSummaryDate);
    Account_Id = AccountDetails.getInstance(mContext).getClientCodes().get(0);
        mDateTxtView.setText("");
            sendRequest(Account_Id);
}
}

这里的问题是,当刷出5-6个 fragment 并刷回到第一个 fragment 时,我得到了NullPointerException。

当我尝试调试时,我发现



为什么这样?

请帮我。

这是我的Fragment包含FragmentPagerAdapter,
public class MyPageDetailViewFragment  extends CommonFragment  {
private View mView;
Context mContext;
OnPageChangeListener pageChangeListener;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.my_page_fragments, null);
    mContext = getActivity();

    setHasOptionsMenu(false);

    setupSearchBarComponent();


    PagerTabStrip pagerTabStrip = (PagerTabStrip)mView. findViewById(R.id.pager_title_strip);
    pagerTabStrip.setDrawFullUnderline(true);
    pagerTabStrip.setTabIndicatorColor(Color.BLACK);

    /** Getting a reference to the ViewPager defined the layout file */
    ViewPager pager = (ViewPager) mView.findViewById(R.id.pager);

    /** Getting fragment manager */
    FragmentManager fm = ((HomeScreenActivity)mContext).getSupportFragmentManager();

    /** Instantiating FragmentPagerAdapter */
    final MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

    pager.setAdapter(pagerAdapter);

    pager.setCurrentItem(0);


    pageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int selectedIndex) {
            System.out.println("selectedIndex = "+selectedIndex);
            Object page  = pagerAdapter.getItem(selectedIndex);
            if( page instanceof OnPageChangeListener){
                ((OnPageChangeListener) page).onPageSelected(selectedIndex);
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    };



    pager.setOnPageChangeListener(pageChangeListener);

    return mView;
    //      return super.onCreateView(inflater, container, savedInstanceState);
}

这是我的实现MyFragmentPagerAdapter,
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {


/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
}

/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int index) {

    System.out.println("getItem = "+index);
    Bundle data = new Bundle();
    data.putInt("current_page", index + 1);

    CommonFragment fragment = getFragment(index);

    fragment.setArguments(data);

    return fragment;
}


public CommonFragment getFragment(int index) {
    CommonFragment fragment = null;
    if(fragment == null){
        if(index == 0){
            fragment = new MySummaryFragment();
        }else if(index == 1){
            fragment = new FragmentOne();
        }else if(index == 4){
            fragment = new FragmentFour();
        }else if(index == 5){
            fragment = new FragmentFive();
        }else{
            fragment = new MyFragment();
        }
    }
    return fragment;
}

/** Returns the number of pages */
@Override
public int getCount() {
    return titles.length;
}

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

}

最佳答案

pagerAdapter.getItem(selectedIndex)实际上创建了 fragment 的新实例。

看到这个答案:https://stackoverflow.com/a/23843743/1271907

您必须保留对实际 fragment 的引用。

10-08 18:43