Calling Fragment method from Activity,findFragmentById returns null

dsb

I'm new to Fragments. I'm trying to develop swipe tabs.

I have the following Adapter:

public class HomeTabsPagerAdapter extends FragmentPagerAdapter {

    public HomeTabsPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int idx) {
        switch (idx) {
        case CONSTS_MAIN.TAB_WATCH:
            return new WatchFragment();
        case CONSTS_MAIN.TAB_DISCOVER:
            return new DiscoverFragment();
        case CONSTS_MAIN.TAB_LOG:
            return new LogFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return CONSTS_MAIN.HOME_NO_OF_TABS;
    }

}

and MainActivity.java

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
}

and of course, the Fragment classes:

public class WatchFragment extends Fragment {
public class DiscoverFragment extends Fragment {
public class LogFragment extends Fragment {

And basically everything seems to work fine graphically.

However, I'm trying to call method of the Fragment from the Activity. I'm doing it this way:

FragmentManager fm = getSupportFragmentManager();
WatchFragment watchFragment = (WatchFragment) fm.findFragmentById(R.id.watch_fragment);
watchFragment.refresh();

and here fm.findFragmentById always return null.

I'm using android.support.v4.app all the way.

How can I get the Fragment class?

EDIT:

Here is the Fragment's layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id ="@+id/watch_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:label="@string/title_activity_watch"
    android:orientation="vertical" >

    <com.x.y.custom_controls.CustomWatchChronologicalListView
        android:id="@+id/lvWatchChronologicalDiscussions"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

and here is the Activity's layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>
Ollie Strevel

Use this:

In your adapter add a SparseArray to keep references to the fragments added

private final SparseArray<Fragment> mPageReferences = new SparseArray<Fragment>();

In your getItem method after instantiate the fragment add it to the array:

@Override
public android.support.v4.app.Fragment getItem(int idx)
{
    Fragment fragment = null;

    switch (idx)
    {
        case CONSTS_MAIN.TAB_WATCH:
            fragment = new WatchFragment();
        case CONSTS_MAIN.TAB_DISCOVER:
            fragment = new DiscoverFragment();
        case CONSTS_MAIN.TAB_LOG:
            fragment = new LogFragment();
    }
    if(fragment != null) {
        mPageReferences.put( position, fragment );
    }
    return fragment;
}

And add the followings methods:

@Override
    public void destroyItem(ViewGroup container, int position, Object object)
    {
        super.destroyItem(container, position, object);
        mPageReferences.remove(position);
    }

    public SherlockFragment getFragment(int key) {
        return mPageReferences.get(key);
    }

Finally you can get the fragment in the index you want like this:

Fragment fragment = (Fragment)adapter.getFragment(index);

If you want to use keys to identify the fragments instead int index, you can use a HashMap

Good Luck!

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 when calling a Fragment method from Activity?

From

Calling a Fragment method from a parent Activity

From Dev

calling parent activity method from fragment

From Dev

NullPointer when calling Fragment method from activity

From Dev

Calling fragment method from activity Android

From Dev

Calling findViewById in fragment returns null

From Dev

Call fragment method from activity but view is null

From Dev

findFragmentById returns null for fragment after migration to AndroidX

From Dev

Calling Activity from Fragment

From Dev

Calling fragment from activity

From Dev

Calling Fragment's method from Activity doesn't work?

From Dev

Calling a fragment method from an Adapter

From Dev

Calling method from Fragment in ViewPager

From Dev

Calling a Fragment method from a singleton

From Dev

Starting fragment activity returns null - for getactivity in the fragment

From Dev

Calling a custom fragment from an activity

From Dev

Calling Activity method inside Fragment that is not attached to Activity

From Dev

calling fragment method in Activity not working kotlin

From Dev

calling an activity from fragment activity through Intent

From Java

Calling a method in one fragment from another

From Dev

Calling fragment method from a separate asynctask

From Dev

Call method in fragment from another fragment/activity

From

Call an activity method from a fragment

From Dev

Fragment method not accessible from Activity

From Dev

Call Fragment method from Activity

From Dev

App Crashing when calling a Fragment from an Activity

From Java

Calling a function when an activity is returned to from fragment

From Dev

Navigate back to calling Fragment from another activity

From Dev

Calling Fragment from an activity and updating layout

Related Related

  1. 1

    NullPointerException when calling a Fragment method from Activity?

  2. 2

    Calling a Fragment method from a parent Activity

  3. 3

    calling parent activity method from fragment

  4. 4

    NullPointer when calling Fragment method from activity

  5. 5

    Calling fragment method from activity Android

  6. 6

    Calling findViewById in fragment returns null

  7. 7

    Call fragment method from activity but view is null

  8. 8

    findFragmentById returns null for fragment after migration to AndroidX

  9. 9

    Calling Activity from Fragment

  10. 10

    Calling fragment from activity

  11. 11

    Calling Fragment's method from Activity doesn't work?

  12. 12

    Calling a fragment method from an Adapter

  13. 13

    Calling method from Fragment in ViewPager

  14. 14

    Calling a Fragment method from a singleton

  15. 15

    Starting fragment activity returns null - for getactivity in the fragment

  16. 16

    Calling a custom fragment from an activity

  17. 17

    Calling Activity method inside Fragment that is not attached to Activity

  18. 18

    calling fragment method in Activity not working kotlin

  19. 19

    calling an activity from fragment activity through Intent

  20. 20

    Calling a method in one fragment from another

  21. 21

    Calling fragment method from a separate asynctask

  22. 22

    Call method in fragment from another fragment/activity

  23. 23

    Call an activity method from a fragment

  24. 24

    Fragment method not accessible from Activity

  25. 25

    Call Fragment method from Activity

  26. 26

    App Crashing when calling a Fragment from an Activity

  27. 27

    Calling a function when an activity is returned to from fragment

  28. 28

    Navigate back to calling Fragment from another activity

  29. 29

    Calling Fragment from an activity and updating layout

HotTag

Archive