ViewPager NullPointerException while modifying FragmentActivity in Fragment

UserK

I'm trying to modify an old FragmentActivity hosting a view pager with 3 listview in a Fragment. The Fragment class has code that is similar to an Activity. In fact, when converting an existing Android application to use fragments, I though it would have been sufficient to move code from the activity's callback methods into the respective callback methods of the fragment. But it didn't work. I get a NullPointerException at the line

mViewPager.setAdapter(mAppSectionsPagerAdapter);

here is the code:

public class FragmentAllEvents extends Fragment implements ActionBar.TabListener 
{
    AppSectionsPagerAdapter mAppSectionsPagerAdapter;

    // JSON Node names
    private static final String TAG_UID = "uid";
    private static final String TAG_LOGO = "logo";
    private static final String TAG_POKUID = "pokuid";

    static ArrayList<HashMap<String, String>> userList;
    ArrayList<HashMap<String, String>> userListTotal;
    HashMap<String, String> userSelected;
    EventsFunctions eventsFunctions;
    UserFunctions userFunctions;
    /**
     * The {@link ViewPager} that will display the three primary sections of the app, one at a
     * time.
     */
    ViewPager mViewPager;
    static ListView lv;
    ActionBar actionBar;

    //Context context = this;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        View v = inflater.inflate(R.layout.all_events_main, container, false);

        Log.e("AllEventsFragmentActivity Report", "Entering AllEventsFragments");
        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        //FragmentManager fm = getSupportFragmentManager();
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getFragmentManager());

        // Set up the action bar.
        actionBar = getActivity().getActionBar();

        // Specify that the Home/Up button should not be enabled, since there is no hierarchical
        // parent.
        actionBar.setHomeButtonEnabled(true);

        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) getActivity().findViewById(R.id.pager_main);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        // Defining a listener for pageChange 
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() 
        {
            [...]
        });

        [...]
}

In the previous version of this class I was extending FragmentAllEvents with a FragmentActivity instead of a Fragment.

prijupaul

It appears like, mViewPager is not populated and is NULL. If pager_main is in all_events_main layout, you need to use v.findViewById(R.id.pager_main)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NullPointerException with Fragment and ViewPager

From Dev

Get id of TextView in Fragment from FragmentActivity in ViewPager

From Dev

Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

From Dev

NullPointerException a call tag fragment ViewPager

From Dev

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

From Dev

Nullpointerexception on dispatchDraw with ViewPager in nested fragment with PageTransformer

From Dev

Nullpointerexception on dispatchDraw with ViewPager in nested fragment with PageTransformer

From Dev

NullPointerException when adding a new Fragment to a ViewPager

From Dev

NullPointerException while using a setter on fragment

From Dev

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

From Dev

NullPointerException in CustomListAdapter while setting the adapter from a Fragment

From Dev

Call FragmentActivity from Fragment

From Dev

fragment run before fragmentActivity

From Dev

Android problems with fragment and fragmentActivity

From Dev

Refresh Fragment in FragmentActivity

From Dev

Fragment transaction with a FragmentActivity instead of a Fragment

From Dev

Using ViewPager and PagerTabStrip - extending FragmentActivity

From Dev

Fragment in viewpager

From Dev

Activity has been destroyed while adding a Fragment to a ViewPager

From Dev

app crashes while opening new fragment inside viewpager

From Dev

NullPointerException on calling Activity from FragmentActivity

From Dev

Android - Intent Acitivity to a Fragment of a FragmentActivity

From Dev

refresh fragment UI from fragmentActivity

From Dev

Import text from fragmentactivity to fragment

From Dev

FragmentActivity not showing the right layout for the fragment

From Dev

refresh fragment UI from fragmentActivity

From Dev

Open new Fragment from FragmentActivity

From Dev

Fragment inside a fragment with ViewPager

From Dev

onCreateOptionsMenu inside Fragment - Nullpointerexception while getting menu items

Related Related

  1. 1

    NullPointerException with Fragment and ViewPager

  2. 2

    Get id of TextView in Fragment from FragmentActivity in ViewPager

  3. 3

    Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

  4. 4

    NullPointerException a call tag fragment ViewPager

  5. 5

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

  6. 6

    Nullpointerexception on dispatchDraw with ViewPager in nested fragment with PageTransformer

  7. 7

    Nullpointerexception on dispatchDraw with ViewPager in nested fragment with PageTransformer

  8. 8

    NullPointerException when adding a new Fragment to a ViewPager

  9. 9

    NullPointerException while using a setter on fragment

  10. 10

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

  11. 11

    NullPointerException in CustomListAdapter while setting the adapter from a Fragment

  12. 12

    Call FragmentActivity from Fragment

  13. 13

    fragment run before fragmentActivity

  14. 14

    Android problems with fragment and fragmentActivity

  15. 15

    Refresh Fragment in FragmentActivity

  16. 16

    Fragment transaction with a FragmentActivity instead of a Fragment

  17. 17

    Using ViewPager and PagerTabStrip - extending FragmentActivity

  18. 18

    Fragment in viewpager

  19. 19

    Activity has been destroyed while adding a Fragment to a ViewPager

  20. 20

    app crashes while opening new fragment inside viewpager

  21. 21

    NullPointerException on calling Activity from FragmentActivity

  22. 22

    Android - Intent Acitivity to a Fragment of a FragmentActivity

  23. 23

    refresh fragment UI from fragmentActivity

  24. 24

    Import text from fragmentactivity to fragment

  25. 25

    FragmentActivity not showing the right layout for the fragment

  26. 26

    refresh fragment UI from fragmentActivity

  27. 27

    Open new Fragment from FragmentActivity

  28. 28

    Fragment inside a fragment with ViewPager

  29. 29

    onCreateOptionsMenu inside Fragment - Nullpointerexception while getting menu items

HotTag

Archive