GoogleMap complains of being a null object reference - android

I am working on an android application where I use maps and this is the error that I am getting
Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMapType(int)' on a null object reference
Prior to posting my query here I have looked for other similar issues but my code already have all what is being suggested so I was hoping if someone could point out the error.
This is the line which throws the error(second line).
I have no idea why it is caling map null when I've assigned it (at least I think I have)
private void initViews() {
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
//map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
and also this is my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="65dp" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

Use getMapAsync(...) instead of getMap().
Example can be found here.

Related

Google support Map crashing

Google SupportMapFragment is crashing when executing this
( (SupportMapFragment)getChildFragmentManager().findFragmentById(
R.id.map)).getMapAsync(googleMap -> presenter.onMapReady(googleMap, getContext()));
with the nullpointer and the stacktrace is:
java.lang.NullPointerException: Attempt to invoke virtual method 'int
android.graphics.Bitmap.getWidth()' on a null object reference
at com.google.maps.api.android.lib6.gmm6.vector.gl.aj.b(:com.google.android.gms.dynamite_mapsdynamite#214516081#21.45.16
(150400-0):2)
at com.google.maps.api.android.lib6.gmm6.vector.gl.aj.(:com.google.android.gms.dynamite_mapsdynamite#214516081#21.45.16
(150400-0):2)
at com.google.maps.api.android.lib6.gmm6.vector.gl.g.(:com.google.android.gms.dynamite_mapsdynamite#214516081#21.45.16
(150400-0):18)
at com.google.maps.api.android.lib6.gmm6.vector.bs.d(:com.google.android.gms.dynamite_mapsdynamite#214516081#21.45.16
(150400-0):14)
at com.google.maps.api.android.lib6.gmm6.vector.av.run(:com.google.android.gms.dynamite_mapsdynamite#214516081#21.45.16
(150400-0):44)
xml for the supportfragment is
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
It looks like Google Map issue. Clearing the app cache helps me.

Android: How to mark on Google Map in custom dialog?

what I am trying is: to show the google map with starting and end point marks in alert dialog on external activity.
So I did:
1) Create a layout, my_dialog_view.XML for a dialog:(NOTE: this layout is apart from the main layout for the activity where a dialog is called)
<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="com.example.exampleApp">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
2) Define a view for the dialog layout:
LayoutInflater factory = LayoutInflater.from(this);
v = factory.inflate(R.layout.my_dialog_view, null);
3) Define Google map handler:
GoogleMap gmap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
4) Set markers of starting and end point:
LatLng startLoc = new LatLng((double)lat,(double)lng);
CameraPosition camPos = new CameraPosition.Builder().target(startLoc)
.zoom(15).bearing(45).tilt(70).build();
CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);
gmap.animateCamera(camUpd3);
MarkerOptions markerOpts = new MarkerOptions().position(startLoc).title(
"Start Location");
gmap.addMarker(markerOpts);
And I have an error as follows:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
It makes sense to have this error since the layout set for the activity (where the java code are written) does not have the fragment component that I tried to use in 3) step.
Then my question is: How to properly define a google map handler with the external dialog layout (by using LayoutInflater or any other possible solution) to resolve the null reference error?
Any input will be GREATLY appreciated.
Best,

googlemap class in googlemapv2

i am currently working with googlemap v2. the problem is that i can't use the class "googlemap" it shows an error. i am using google api 4.2. how can i use this class. xml file code is given below
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
If you use
class="com.google.android.gms.maps.SupportMapFragment"
Then you have to use the SupportMapFragment class in your activities.
Example:
SupportMapFragment mapa;
mapa = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
Hope it helps
You need to reference GooglePlay services in your android project and import
import com.google.android.gms.maps.GoogleMap;
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap
You can follow
http://developer.android.com/google/play-services/setup.html
and Reference
Importing google-play-service library showing a red X next to this reference android

setContentView() giving error

setContentView() is giving error in below is my code :
public class LocateUserInMap extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locate_user_in_map);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
final LatLng CIU = new LatLng(19.111693900000000000,72.911271500000000000);
mMap.addMarker(new MarkerOptions().position(CIU).title("Friend"));
// findDirections(19.111693900000000000,72.911271500000000000,19.128203900000000000,72.928065000000060000, GMapV2Direction.MODE_DRIVING);
}
}
error :
here is my library that I am using :
and activity_locate_user_in_map.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LocateUserInMap" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
what I am missing...
Please help!!!
Regards,
Sourabh
Your layout file R.layout.activity_locate_user_in_map is missing, maybe it is on another project or it is in correct project but somehow it can't be resolved, try to clean the project
The #SuppressLint("NewApi") really shouldnt be needed for onCreate() so I am going to guess that you have set your project to the something like:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
And then are trying to use Fragments without using the support library.
You will need to change the imports to something like:
import android.support.v4.app.FragmentActivity;
And change any calls to:
getFragmentManager()
to
getSupportFragmentManager()
You might also like to checkout this question for a good background on Fragment, FragmentActivity and the support lib

How to know if Google maps is ready to be used?

I have a simple activity with the follow segment code on it :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:gravity="center_horizontal"
android:orientation="horizontal"
android:id="#+id/title_complex">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"
tools:context=".MainActivity" />
</LinearLayout>
How to check in MainActivity.java if the map is fully loaded and ready ?
Before you can interact with a GoogleMap object, you will need to confirm that an object can be instantiated, and that the Google Play services components are correctly installed on the target device. You can verify that the GoogleMap is available by calling the
MapFragment.getMap()
or
MapView.getMap()
methods and checking that the returned object is not null.
An example of a test to confirm the availability of a GoogleMap is shown below. This method can be called from both the onCreate() and onResume() stages to ensure that the map is always available.
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
}
}
}
References - https://developers.google.com/maps/documentation/android/map
Use GooglePlayServicesUtil.isGooglePlayServicesAvailable.

Categories

Resources