gpt4 book ai didi

android - 在 onPageSelected() 中访问时 fragment 成员变量变为空

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:48:00 26 4
gpt4 key购买 nike

我将 FragmentPagerAdapter 与 FragmentActivity 结合使用来创建可滑动的 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。

当我尝试调试时,我发现

mMyView is null ? = true

为什么会这样?

请帮帮我。

这是我的 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 的引用。

关于android - 在 onPageSelected() 中访问时 fragment 成员变量变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14223270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com