I have a google map
in a fragment
in my application. Funny thing is that the map loads, but the logcat displays a NullPointerException
, and I only noticed it when tried to update the camera on the map so that it could automatically zoom in on the marker
. Only then the map will crash. Otherwise, the map loads fine in the fragment
but with no marker
(even though I am adding one using my current gps location
).
EDITED*
I understand that its crashing because the googleMap
is null, but I don't understand why it's returning null. The map renders and well I've done this before and it usually crashes at that point. Doesn't render.
Here is the logcat error
05-08 01:30:33.143 30130-30130/com.example.javed_000.famjam E/mapApp﹕ 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
05-08 01:30:33.152 30130-30130/com.example.javed_000.famjam E/MapFragment﹕ lat = 10.7270913 and Long = -61.5544451
05-08 01:30:33.155 30130-30130/com.example.javed_000.famjam D/AndroidRuntime﹕ Shutting down VM
05-08 01:30:33.156 30130-30130/com.example.javed_000.famjam E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.javed_000.famjam, PID: 30130
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.animateCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at com.example.javed_000.famjam.MapFragment.getLocation(MapFragment.java:60)
at com.example.javed_000.famjam.MapFragment.onCreateView(MapFragment.java:33)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
at com.example.javed_000.famjam.SlidingTabLayout$TabClickListener.onClick(SlidingTabLayout.java:283)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.javed_000.famjam" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAhgnZ7ESL-EIRS4Da_hGw_7u9IQI3SZE0" />
<activity
android:name=".SignInActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
This is my dependencies in the gradle file
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile 'com.google.android.gms:play-services-maps:7.0.0'
}
This is my layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Here is my code snippet from the Map Fragment
private void createMapView(){
try {
if(googleMap == null){
SupportMapFragment smf = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapView);
googleMap = smf.getMap();
if(googleMap == null) {
Toast.makeText(getActivity().getBaseContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){
Log.e("mapApp", exception.toString());
}
}
private void getLocation(){
gps = new GPSTracker(this.getActivity());
if(gps.CanGetLocation()){
latitude = gps.getLatitude();
longitude = gps.getLongitude();
Log.e("MapFragment","lat = " + latitude + " and Long = " + longitude);
addMarker(latitude,longitude);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude), 12));
} else {
gps.showSettingsAlert();
}
}
private void addMarker(double lat, double lng){
if(googleMap != null){
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Marker")
.draggable(true)
);
}
}
This worked for me.
getSupportFragmentManager()
seemed to be causing all the crashes.MainActivity.xml :
MainActivity.java :
Here's the permission:
And here's the metadata section: