0

is it possible to get access from googlemap(SupportMapFragment) in other class?

i test several method and get follow errors:

map = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

return:

java.lang.NullPointerException


map = ((SupportMapFragment)  ((FragmentActivity)context).getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

return:

android.app.Application cannot be cast to android.support.v4.app.FragmentActivity

note : i cant change class to extends Fragment

hamedata
  • 125
  • 1
  • 12
  • 1
    `other class ?` means – M D Apr 24 '15 at 12:15
  • @hamedata what do you mean when you say another class? Do you mean from an activity? fragment? – Alexios Karapetsas Apr 24 '15 at 12:15
  • use this ((SupportMapFragment) getActivity() .getSupportFragmentManager() .findFragmentById(R.id.map_view)).getMap(); – Sree Reddy Menon Apr 24 '15 at 12:17
  • sorry, i mean run in class (not activity). when i try to get "SupportMapFragment" in activity its fine. but when i try to do same in class i got above errors! – hamedata Apr 24 '15 at 12:22
  • @Sree Reddy Menon with your suggest code i get "The method getActivity() is undefined for ..." – hamedata Apr 24 '15 at 12:23
  • pass context to your other class with a constructor like how we do in adapter of listview. – Sree Reddy Menon Apr 24 '15 at 12:27
  • How are you passing your `context` ? If you calling `getApplicationContext()`, you will see this issue.. Use your calling `Activity`'s `Context` view `MyActivity.this` or `this` – kandroidj Apr 24 '15 at 12:30
  • for pass context to class i use : `gLocation = new GLocation(context);` in activity and in class i use `public GLocation(Context _context) { this.context = _context; }` – hamedata Apr 24 '15 at 12:35

1 Answers1

1

If you want to call it from an Activity you have to extend the FragmentActivity from the support library. Otherwise, and if you are targeting API level 11 and above the static method getFragmentManager could do the job(Activity.getFragmentManager)

  • my class extend fragmentactivity (public class gLocation extends FragmentActivity) but i cant get map with map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); – hamedata Apr 24 '15 at 12:25
  • 2
    @hamedata Have a look here http://stackoverflow.com/questions/14067889/google-play-service-supportmapfragment-getmap-always-returning-null – Alexios Karapetsas Apr 24 '15 at 13:21