Google Maps returns null - android

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" />

Related

Call MapActivity to MainActivity with markers android

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" />

Google Maps Display

Iam practicing on Google Maps for android development. I have followed the steps as in https://developers.google.com/maps/documentation/android-api/map-with-marker
While when playing the app I got the screen like this below, no marker and the map cannot show any thing
My code as following:
public class MainActivity extends AppCompatActivity 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 googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
******** . XML Layout ************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ae.map_04.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
This is most likely an API Key issue. You can find some guidance here: Android Studio - Google map still blank on real Android device on release apk
If that does not work, try a new API Key.

Google maps error inflating, Android

I get the following error when I try to leave fragment which contains map (on back button press).
ERROR: mapInflater->Binary XML file line #7: Error inflating class fragment
Bellow is onCreate() method of fragment where the map is placed. And also there is a xml code of layout.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
apiRequestManager = ApiRequestManager.getInstance();
fragmentHandler = FragmentHandlerActivityMain.getInstance(getActivity());
session = new SessionManager(App.getInstance().getApplicationContext());
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.maps_fragment_toolbar_name));
Handler mainHandler = new Handler();
Runnable myRunnable = new Runnable() {
#Override
public void run() {
try {
SupportMapFragment maps = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.mapView);
if (maps != null)
maps.getMapAsync(MapsFragment.this);
} catch (Exception ex) {
LogUtil.getInstance().add(LogUtil.ERROR,
TAG, "onCreateMaps", ex);
}
}
};
mainHandler.post(myRunnable);
}
Log is pointing on this layout on fragment (line 7).
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app1="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".salesman.fragments.MapsFragment">
<fragment
android:id="#+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</FrameLayout>
You are trying to getChildFragmentManager() instade of getSupportFragmentManager().
Need to change In Java File :
1). You need to implements OnMapReadyCallback in Your Fragment.
2). Get SupportMapFragment from View.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
3). Overrid onMapReady(GoogleMap googlMap) Method.
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// and move the map's camera to the same location.
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
In XML File :-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app1="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".salesman.fragments.MapsFragment">
<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"/>
</FrameLayout>
Please check the updated answer as per below comment:
Now try to set MapView insted of fragment because if you use fragment inside fragment it creates lot of issues.
Need to change in JAVA Code : // Initialize GoogleMap myMap; MapView mMapView;
// OnCreateView Method MapsInitializer.initialize(this.getActivity()); mMapView = (MapView) rootView.findViewById(R.id.map); mMapView.onCreate(savedInstanceState); mMapView.getMapAsync(this);
#Override public void onPause() { super.onPause(); mMapView.onPause(); }
#Override public void onPause() { super.onPause(); mMapView.onPause(); }
#Overridepublic void onResume() { super.onResume(); mMapView.onResume(); }
Need to change in XML File :
<com.google.android.gms.maps.MapView android:id="#+id/map" android:layout_width="match_parent" android:layout_height="match_parent" />

show marker of current position in google maps

I want to show the current position in google maps with a marker inside a Fragment class. The map is already shown, but I get a NullpointerException, when I initialize the MapFragment.
I tried it with this initialization:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
This is the onMapReady() function:
#Override
public void onMapReady(GoogleMap googleMap) {
this.googlemap_global = googleMap;
googlemap_global.setMyLocationEnabled(true);
googlemap_global.setTrafficEnabled(true);
googlemap_global.setIndoorEnabled(true);
googlemap_global.setBuildingsEnabled(true);
googlemap_global.getUiSettings().setZoomControlsEnabled(true);
}
And this is the layout 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"
android:orientation="horizontal"
tools:context=".GPSTracker2" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="400dp"
class="com.google.android.gms.maps.MapFragment" />
<Button
android:id="#+id/gps_service_start_stop_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GPS Service starten"/>
Here I set the Markers:
LatLng latlng = new LatLng(latitude_global,longitude_global );
if(googlemap_global != null){
Marker aktueller_standort = googlemap_global.addMarker(new MarkerOptions().position(latlng).title("Standort"));
googlemap_global.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));
// Zoom in, animating the camera.
googlemap_global.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
Here is the logcat output:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.app.Fragment android.app.FragmentManager.findFragmentById(int)' on a null object reference
at com.gpstracker.viktor.gpstracker2.Home_Fragment.<init>(Home_Fragment.java:82)
at com.gpstracker.viktor.gpstracker2.GPSTracker2.selectItemFromDrawer(GPSTracker2.java:105)
at com.gpstracker.viktor.gpstracker2.GPSTracker2.access$000(GPSTracker2.java:28)
at com.gpstracker.viktor.gpstracker2.GPSTracker2$1.onItemClick(GPSTracker2.java:67)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
at android.widget.AbsListView$3.run(AbsListView.java:3903)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Thanks for helping.
Your XML File
<FrameLayout
android:id="#+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java file code
FrameLayout mapContainer = (FrameLayout) findViewById(R.id.mapContainer);
//show map of 40% of screen
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params.height = (int) ((float) dm.heightPixels * (float) 0.5);
mapContainer.setLayoutParams(params);
FragmentManager fm = getFragmentManager();
MapFragment mapFragment = MapFragment.newInstance();
fm.beginTransaction().replace(R.id.mapContainer, mapFragment).commit();
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
GoogleMap mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
addMarks(mMap);
Show marker on map
private void addMarks(GoogleMap googleMap) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLng latLng = new LatLng(lat1, long1);
builder.include(latLng);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(currentLocation);
googleMap.addMarker(markerOptions);
}
Change this like below
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="400dp"
class="com.google.android.gms.maps.MapFragment" />
to
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="400dp"
class="com.google.android.gms.maps.SupportMapFragment" />
and
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
to
SupportMapFragment branchdetails_map =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

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();

Categories

Resources