How to call a fragment layout from the FragmentActivity to avoid NullPointerException

Moh'd Al-Ahmad

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.

Dmide

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call FragmentActivity from Fragment

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

Android - How to access a Fragment from a FragmentActivity

From Dev

How to pass parameter to Fragment from FragmentActivity

From Dev

FragmentActivity not showing the right layout for the fragment

From Dev

ViewPager NullPointerException while modifying FragmentActivity in Fragment

From Dev

Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

From Dev

NullPointerException on calling Activity from FragmentActivity

From Dev

refresh fragment UI from fragmentActivity

From Dev

Import text from fragmentactivity to fragment

From Dev

refresh fragment UI from fragmentActivity

From Dev

Open new Fragment from FragmentActivity

From Dev

How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

From Dev

How to include FragmentActivity layout in Activity layout?

From Dev

Android - How to call a fragment from inside a fragment?

From Dev

How to call Fragment from Fragment along with data

From Dev

How to call the AsyncTask in the fragment from other fragment?

From Dev

How to avoid NullPointerException from arithmetic operators in Java?

From Dev

how to call a fragment from activity

From Dev

How to call AppCompatDialogFragment from Fragment

From Dev

How to call fragment from adapter

From Dev

How to call dialog from fragment

From Dev

How to call a class from a fragment

From Dev

How to avoid TalkBack from reading dismissed fragment

From Dev

Get id of TextView in Fragment from FragmentActivity in ViewPager

From Dev

Android - Back button from FragmentActivity to another Fragment

From Dev

Fragment being launched from FragmentActivity not showing up

From Dev

Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

Related Related

  1. 1

    Call FragmentActivity from Fragment

  2. 2

    (Android) how to call method in fragment from fragmentActivity

  3. 3

    How to call fragment from activity without using fragmentactivity?

  4. 4

    Android - How to access a Fragment from a FragmentActivity

  5. 5

    How to pass parameter to Fragment from FragmentActivity

  6. 6

    FragmentActivity not showing the right layout for the fragment

  7. 7

    ViewPager NullPointerException while modifying FragmentActivity in Fragment

  8. 8

    Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

  9. 9

    NullPointerException on calling Activity from FragmentActivity

  10. 10

    refresh fragment UI from fragmentActivity

  11. 11

    Import text from fragmentactivity to fragment

  12. 12

    refresh fragment UI from fragmentActivity

  13. 13

    Open new Fragment from FragmentActivity

  14. 14

    How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

  15. 15

    How to include FragmentActivity layout in Activity layout?

  16. 16

    Android - How to call a fragment from inside a fragment?

  17. 17

    How to call Fragment from Fragment along with data

  18. 18

    How to call the AsyncTask in the fragment from other fragment?

  19. 19

    How to avoid NullPointerException from arithmetic operators in Java?

  20. 20

    how to call a fragment from activity

  21. 21

    How to call AppCompatDialogFragment from Fragment

  22. 22

    How to call fragment from adapter

  23. 23

    How to call dialog from fragment

  24. 24

    How to call a class from a fragment

  25. 25

    How to avoid TalkBack from reading dismissed fragment

  26. 26

    Get id of TextView in Fragment from FragmentActivity in ViewPager

  27. 27

    Android - Back button from FragmentActivity to another Fragment

  28. 28

    Fragment being launched from FragmentActivity not showing up

  29. 29

    Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

HotTag

Archive