2014-07-03 58 views
0
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) { 
     } 
    }); 

更新FragmentFavoritesViewPager更新分段

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空指針),儘管事實上,我發現它在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

你能告訴你如何膨脹'rootView',你確定'rootView'實際上是保存'textview_fact'的正確視圖嗎? – Darwind

+0

一切工作正常,如果我不打電話**更新** – makaron

回答

0

當您新建FragmentFavorites實例時,onCreateView()尚未調用。您應該從ViewPager找到FragmentFavorites實例,而不是創建一個實例。