Вопрос

I am using action bar tabs with FragmentPagerAdapter. (3 tabs) I have onCreateOptions menu set at the MainActivity that extends FragmentActivity.

This menu contains a save button that runs saveData() method on the MainActivity, which saves the data in the whole 3 fragments.

The problem is that when I first open the app and hit the save button, I get NPE when this method tries to save data on the 3rd fragment. I know that this is because onCreateView() method is not called yet, and when I switch tabs, the saveData() method works fine.

The question is, how can I avoid the NPE? or how can I call the layout of the 3rd fragment so that saveData() can access its views.

Это было полезно?

Решение

Well, the simplest way would be to just check your Fragments views for null inside your saveData() method. If it is necessery to save data from every fragment, you can use this method on your ViewPager to force it to load all fragments at once.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top