viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
if(position == 1)
{
new FragmentFavorites().update(getApplicationContext());
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
来自 FragmentFavorites 的更新
public void update(Context context) {
savedFacts = com.invizorys.sexpro.util.Util.readFactsList(context);
if (savedFacts.size() > 0)
mTextView.setText(savedFacts.get(currentFactIndex));
else
mTextView.setText("you still have not saved any fact");
}
当我打开FragmentFavorites应用程序崩溃(mTextView nullpointer)时,尽管我在onCreateView()中找到它
mTextView = (TextView) rootView.findViewById(R.id.textview_fact);
更新
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_favorites, container, false);
...
mTextView = (TextView) rootView.findViewById(R.id.textview_fact);
...
return rootView;
}
答案 0 :(得分:0)
当您新建FragmentFavorites实例时,尚未调用onCreateView()。您应该从ViewPager中找到FragmentFavorites实例,而不是创建一个。