0

I have an application in which I am attaching different fragment with activity. and in that activity container I am replacing different fragment.

So at the start of the activity I replace the MapFragment in the activity container. Later on I replace other fragment and then from main activity I replace again the map fragment. Although I think I have wrong implementation of map in fragment but I wanted to know and wanted to do some different things and I think as I have no better understanding of fragment so I think that is why I am getting null pointer exception

What I have done SO far

Case 1. AS I said On App start I am replacing map fragment but doing nothing more except showing user current location by just setting user location enable in google map v2. so on the button click of the map let say show other location , fetch location information from server and draw pins on the map. this is working good as far as the map fragment is there.

Case2

if user click button let say "Show location" while there is other fragment in the container let say About Us fragment which contains app info , so now on this button it is supposed that map fragment should be replaced again and then the function in that fragment which is going to download locations from the server and to show them on map should start working as I am calling on button click

  fragmentTransaction1.replace(R.id.frame, myMapFragment, "MyMapFragment").commit();
                        fragmentManager.executePendingTransactions();
                        myMapFragment.GetAllLocations("friendsLocation");

so this GetLocations() should run , but at this stage the app crash saying null pointer exception. and in this function myMapFragment.GetAllLocations("friendsLocation") there is a need of context which I taking as getActivity(), the context is needed as I have to make a call to web api using ION library which needs context.

So these are my basic questions

  1. How can I get to know that fragment is started and then the function which I have called from MainActivity should run after the mapFragment is loaded and started so that I can not get the null context in the getActivity() ?

    1. Is there any other way of doing that , I hope you understand my need that is , I want to replace the map fragment and I want to call the fragment method on the same time by passing the parameters to fragment method from the main activity , but How can it be done ?

    2. An idea is in my mind and that is while replacing back the map fragment through Mainactivity button , i should set some static value in the mapFragment let say

public static boolean isLocationShouldDownload== flase //in map fragment

I should set it true while replacing fragment and then when the fragment is started after it is in running condition it should check this variable if it is true (set by the main activity) then it should download and start plotting pins. but I have no idea how can I get to know that fragment is running .

I have no idea how and where I have to implement my idea which I have stated in point no 3.

Please share your idea with me and give me suggestion or share me a source code that how can I achieve this task through main activity that fragment should replace and download the locations and to show them on map without getting app crash.

Coas Mckey
  • 701
  • 1
  • 13
  • 39
  • Fragment myMapFragment= new MyMapFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.frame, myMapFragment, "myMapFragment").commit(); – Nithinlal Sep 14 '15 at 06:33
  • if you are using android.support.v4.app.Fragment u have to use getSupportFragmentManager(); – Nithinlal Sep 14 '15 at 06:36
  • what are you saying I am using exactly same line to replace my map Fragment in the main container , the problem is getting the context and to call that function – Coas Mckey Sep 14 '15 at 06:43
  • where is your button in the fragment xml or the main xml – Nithinlal Sep 14 '15 at 06:49
  • main xml in main activiyt – Coas Mckey Sep 14 '15 at 06:56
  • actually it is in navigation drawer – Coas Mckey Sep 14 '15 at 06:57
  • so You have navigation drawer fragment and the content fragment in the main activity – Nithinlal Sep 14 '15 at 06:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89555/discussion-between-nithinlal-and-coas-mckey). – Nithinlal Sep 14 '15 at 06:58
  • The better option to notify change to the activity in this case is use LocalBroadcastManager you can't change the content fragment from the drawer fragment. You have to sent a value to activity to notify the content has to be changed from there you can change the fragment – Nithinlal Sep 14 '15 at 07:08

0 Answers0