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();
Related
I have a MapActivity (The default by Android) and a MainActivity. And i call the MapActivity with an include like this at the MainActivity:
<include
android:id="#+id/mapa"
layout="#layout/activity_maps"/>
It shows me the map but not the markers anyone knows why? In the MapActivity i have this:
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
Thanks all!
Use this only dont call include layout it wont work properly
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
I have a problem with Google Maps, i.e. getSupportFragmentManager().findFragmentById returns always null. Do you have an idea how to solve this?
Here is the code:
fragment_map.xml:
<FrameLayout 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"
tools:context="com.myapp.something.MapFragment">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
MapsFragment.java:
public class MapFragment extends Fragment implements OnMapReadyCallback, android.location.LocationListener
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
...
I have Google Maps in Activity and it works with code:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
I am trying to reuse this in fragment, becouse I need maps in fragment, not in activity, but it doesn't work.
I tried:
calling this code in "onCreateView" function
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); is deprecated, and application crashes
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
and similar variations, but in all cases I get null for mapFragment.
Do you know how could I solve this problem?
The problem is that you're trying to use the Activity's FragmentManager, and you should be using the Fragment's child FragmentManager.
Remove the onCreate() override in the Fragment, and add an onCreateView() Override where you inflate the layout and call getMapAsync():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return rootView;
}
I have a Google Maps Activity. suddently it is shutting down because getMap() (java null pointer exeption) doesn't seem to work. I reinstalled some older apks that worked fine and but yet these are shutting down to. So it has to be something on the device or so, what can be the reason?
private GoogleMap googleMap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
if(googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
Here is the xml file
<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"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"/>
Logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.mareike.cityexplorer/de.mareike.cityexplorer.MainActivity}: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at com.google.l.a.ce.a(Unknown Source)
at com.google.maps.api.android.lib6.d.eg.<init>(Unknown Source)
at com.google.maps.api.android.lib6.d.ef.a(Unknown Source)
at com.google.maps.api.android.lib6.d.aq.<init>(Unknown Source)
at com.google.maps.api.android.lib6.d.ft.a(Unknown Source)
at com.google.android.gms.maps.internal.l.onTransact(SourceFile:167)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
at de.mareike.cityexplorer.MainActivity.onCreate(MainActivity.java:88)
According to here, the getMap() method is deprecated and you need to use getMapAsync(OnMapReadyCallback) instead.
Sample code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 10));
}
}
and the activity_main.xml:
<fragment 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:id="#+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
For more details, please refer official doc here.
Check your layout activity_main should contains fragment having id map
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
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();
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().