Mapfragment findFragmentById always null - android

I have problems to access the fragment of the map.
getFragmentManager().findFragmentById(R.id.map)) returns always null.
I don't know why.
What's the problem?
Thank you!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvNombreCentro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tag_fragment_map" />
</LinearLayout>
And in the activity after the setContentView I try to access to the map, but I receive an exception
public class Mapa extends Activity {
private GoogleMap mMap;
private ActionBar ab;
private TextView tvNombreCentro;
private TextView tvTelefonoValor;
private TextView tvEMailValor;
private TextView tvWebValor;
private TextView tvDireccionValor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapa_centro);
GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
}

use this way:
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
map = mapFrag.getMap();
here is full code with sample:

In case your problem is not solved, try this :
As you are using SupportMapFragment, while retrieving map, use
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Also, change
public class Mapa extends Activity {
to
public class Mapa extends FragmentActivity {

I had a similar problem when trying to get MapFragment but getting null because the device didn't have Google Play Services updated.

Change in the layout
android:name = "com.google.android.gms.maps.SupportMapFragment"
by
android:name = "com.google.android.gms.maps.MapFragment".
Or, you can use getSupportFragmentManager() instead of getFragmentManager().

Related

getSupportFragmentManager().findFragmentById return null on GoogleMap

I am simply trying to get my map fragment to then be able to execute getmapasync and draw markers on it. However my mapFragment value is always null. I dont understand why. I read from other answers that it might something to do with the fact that my fragment is inside a frame and therefore is some sort of child.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
startService(new Intent(MainActivity.this, TrackerService.class));
bindService(new Intent(MainActivity.this, TrackerService.class), myConnection, Context.BIND_AUTO_CREATE);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d("DEBUG", "MAP READY");
mMap = googleMap;
drawLocationMarkers();
}
And the XML:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dp"
android:background="#drawable/custom_card"
android:layout_weight="0.5">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/map"
class="com.google.android.gms.maps.MapFragment"/>
</FrameLayout>
You need to change your layout to use SupportMapFragment instead of MapFragment
android:name="com.google.android.gms.maps.SupportMapFragment"

SupportMapFragment return null on Android studio

I move my application eclipse to android studio. My code works perfect on Eclipse but on Android studio
mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
return null
my xml is here
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:ads="http://schemas.android.com/apk/res-auto"
>
<com.google.android.gms.ads.AdView
android:id="#+id/adMob"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_unit_id" />
<fragment
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
why it return null?
I searched and compileSdkVersion 21 cause it. But if I change it 19 still return null
getMap() is not guaranteed to return a map instance. Use getMapAsync(OnMapReadyCallback) where the callback will be invoked once the map is ready and you're guaranteed it won't be null. This is available since Google Play Services library v6.5.
When using this approach get rid of your mapView variable, the callback will receive a map reference anyway. Also note that SupportMapFragment is not MapView is not GoogleMap (I'm referring to the misnamed variable here, be careful.).
EDIT: Suggested solution:
public class MainActivity extends ActionBarActivity {
SupportMapFragment mMapFragment;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mMapFragment = getSupportFragmentManager().findFragmentById(R.id.map);
// etc.
}
public void doSomethingWithMap() {
mMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
// do whatever you need with the map
}
});
}
}
The point is when you need to access the map, you ask the fragment for it, and it will be delivered to you via a callback.

GoogleMap throwing null pointer on getMap();

I am having a FragmentActivity, where I have successfully implemented a Google Map through my XML with this code:
<fragment
android:id="#+id/googleMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
However, if I try to initialize the map to find a persons location with this code:
GoogleMap googleMap;
googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.googleMap))).getMap();
It is throwing a null pointer exception, as I have understood from other questions regarding this error, it shouldn't be a problem initializing the map when it is created in the XML file. And yes I have used
setContentView(R.layout.map);
Before I initialize the map.
You use the Support Fragment So in your XML File Add
this Line In Fragment
class="com.google.android.gms.maps.SupportMapFragment"
like belove.
<fragment
android:id="#+id/googleMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
and Put This
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.googleMap)).getMap();
The error is due to incorrect variable initialization try to change your code as shown below,thanks
private GoogleMap gMap;
MapFragment googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_search);
googleMap= (MapFragment) getFragmentManager().findFragmentById(
R.id.googleMap);
gMap= googleMap.getMap();
In you xml you are using MapFragment.Change it -
<fragment
android:id="#+id/mapfragment"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
class="com.google.android.gms.maps.SupportMapFragment" />
java -
googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();

Using SupportMapFragment in Activity instead of FragmentActivity

I am using SupportMapFragment to show the google map in my activity. But the activity must be declared as FragmentActivity. Is there any way to use it in Activity instead of FragmentActivity? Here is my work:
In xml, I use:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In class file:
public class MapActivity extends FragmentActivity {
Context context;
GPSTracker gps;
Location location;
GoogleMap mMap;
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_map);
gps = new GPSTracker(this);
location = gps.getLocation();
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng((locLat + location.getLatitude()) / 2,
(locLng + location.getLongitude()) / 2), 12.0f));
}
The correct way to do it is:
Put your map inside a fragment and in your activity inflate this fragment.
your xml code:
<com.google.android.gms.maps.MapView
android:id="#+id/map_detail"
android:layout_width="match_parent"
android:layout_height="246dp" />
your java code to put in activity to inflate fragment:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, new MapFragment()).commit();
With this way you can implements Activity in your activity class and Fragment in your fragment class.

Unable to cast Fragment to SupportMapFragment

why is it i could not cast the Fragment to SupportMapFragment.here is the code
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
here is my activity_main.xml
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
It should be getSupportFragmentManager(). Make sure that your Activity extends FragmentActivity for ActionBarActivity from the support package.
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();

Categories

Resources