0

First of all say that this question is not duplicated, since I have seen all the questions and no solution works for me. My problem is that I do not know how to call the "RefreshMapList" method of the fragment from other Activity which i have, thx for your help!

Fragment's method:

public void RefreshMapList(){
        if (MapList == 0) {
            MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.simpleFrameLayout);
            fragment.RefreshMap();
        }
        else if (MapList == 1) {
            Gallery_Fragment fragment = (Gallery_Fragment) getFragmentManager().findFragmentById(R.id.simpleFrameLayout);
            fragment.RefreshList();
        }
        else {
            MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.simpleFrameLayout);
            fragment.RefreshMap();
            Gallery_Fragment fragment2 = (Gallery_Fragment) getFragmentManager().findFragmentById(R.id.simpleFrameLayout);
            fragment2.RefreshList();
        }
    }
Budha 600
  • 33
  • 7
  • Possible duplicate of [Call an activity method from a fragment](https://stackoverflow.com/questions/12659747/call-an-activity-method-from-a-fragment) – Joaquín Oct 19 '18 at 10:58
  • It tells me : java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.Fragment android.support.v4.app.FragmentManager.findFragmentById(int)' on a null object reference – Budha 600 Oct 19 '18 at 11:00
  • "from other Activity"? – Dmytro Oct 19 '18 at 11:01
  • and the app stops working – Budha 600 Oct 19 '18 at 11:01
  • @Budha600 its because youre pointing to a null Fragment, it doesn't even exist when you execute the code, debug it and make sure you're pointing to the rigth ID. – Joaquín Oct 19 '18 at 11:02
  • Yes, an activity called by a menu on the bottom toolbar – Budha 600 Oct 19 '18 at 11:03
  • @JoaquinAlvarez but when I'm in the fragment I do not get an error, only when I'm in the activity and I call that method – Budha 600 Oct 19 '18 at 11:07
  • @Budha600 you're not getting the error in the fragment because the fragment youre pointing is initialized, review your code... – Joaquín Oct 19 '18 at 11:10
  • @JoaquinAlvarez but I do not understand why I start the application and I already get into that fragment, it is by giving it in the lower toolbar menu that an intent opens with that activity in which it calls and the previously opened fragment method – Budha 600 Oct 19 '18 at 11:19
  • @JoaquinAlvarez and when i try to call other method from other fragment it appears: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference – Budha 600 Oct 19 '18 at 11:33
  • @Budha600 That's because Linearlayout Isn't initialized when you want to call the method – Joaquín Oct 19 '18 at 11:35

2 Answers2

1

Your question is duplicate from this

Here's the asnwer you're looking for

FragmentManager fm = getSupportFragmentManager();

//if you added fragment via layout xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();
Joaquín
  • 1,116
  • 1
  • 10
  • 26
0

try this ..

                 Fragment mapFragment = getSupportFragmentManager().findFragmentById(R.id.simpleFrameLayout);
                    if (mapFragment instanceof MapFragment) {
                        ((MapFragment) mapFragment).RefreshMap();
                    }
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47