Discussion Forums  >  Maps, Device Location, Tracking

Replies: 6    Views: 830

Louie
Code is Art
Profile
Posts: 40
Reg: Dec 31, 2012
Toronto
400
12/20/15 11:09 AM (3 years ago)

Google map error

I am trying to update one of my published apps in Android studio that contains a google map, however the app crashes when opening the map.

What Iíve done so far:

Placed my google API key in the AndroidManifest.xml.

Compiled the apk with the keystore I used for the sha1 and tried it on a real device.

Doubled checked the api key and the sha1 code on google developer console.

Error log:
12-20 11:21:57.806 15989-15989/com.myapp E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@34b72b16
12-20 11:21:57.806 15989-15989/com.myapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@2a223c97
12-20 11:21:57.806 15989-15989/com.myapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@2a223c97
12-20 11:21:57.946 15989-15989/com.myapp D/AndroidRuntime: Shutting down VM
12-20 11:21:57.946 15989-15989/com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 15989
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
at com.myapp.BT_screen_map.onCreateView(BT_screen_map.java:181)
at android.app.Fragment.performCreateView(Fragment.java:2114)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6872)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
12-20 11:22:20.606 15989-15989/com.myapp I/Process: Sending signal. PID: 15989 SIG: 9
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
12/20/15 02:16 PM (3 years ago)
>>
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
at com.myapp.BT_screen_map.onCreateView(BT_screen_map.java:181)
>>

The key to successful debugging on Android is to find one or two lines which reference the code in your app and see what is missing in it. Here, it will be line 181 in BT_screen_map.java. Going there, we see that it says in line 176


// get the com.google.android.maps.v2.API_KEY from the
// AndroidManifest.xml file...

From the above log we see that NullPointerException is the problem, i.e. you are referencing something that does not exist.

Conclusion: something is wrong with the API key in the manifest file, have a look there and verify that this is the case or not.
 
Louie
Code is Art
Profile
Posts: 40
Reg: Dec 31, 2012
Toronto
400
like
12/20/15 04:12 PM (3 years ago)
Thanks Dusko for your help but the API key is correct (I copied it directly from google developer console into the manifest file) so it has to be something else.
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
12/20/15 11:12 PM (3 years ago)
>the API key is correct

OK, but what else is wrong? The error

java.lang.NullPointerException:

means that something was used but not initialized. In Java and many other languages, that will mean that there is no pointer to a structure or record that holds the data.

Here is the code around line 181 in bt_screen_map.java:

ApplicationInfo ai = null;
String tmpAPIKey = "";
try {
ai = BT_appDelegate.getApplication().getPackageManager().getApplicationInfo(BT_appDelegate.getApplication().getPackageName(), PackageManager.GET_META_DATA);
tmpAPIKey = (String) ai.metaData.get("com.google.android.maps.v2.API_KEY");
}
catch (NameNotFoundException e) {
BT_debugger.showIt(fragmentName + ":onCreate. EXCEPTION. Google Maps v2 API Key not found in AndroidManifest?");
}


So, to see what is (not) going on, print the value of tmpAPIKey, either using a BT_debugger.showIt statement or by putting a breakpoint and executing in the debug mode.

If that value is nil, try to put the api key value directly into tmpAPIKey and see whether that produces a different outcome. If not, then somethins else is wrong.
 
Louie
Code is Art
Profile
Posts: 40
Reg: Dec 31, 2012
Toronto
400
like
12/21/15 09:02 AM (3 years ago)
Ok I entered the API key directly in the tmpAPIKey and I got a blank screen (no errors).

Doubled checked the API Key and the SHA1 on the google developer console and they're all correct.

So something else must wrong.
 
Louie
Code is Art
Profile
Posts: 40
Reg: Dec 31, 2012
Toronto
400
like
12/30/15 01:37 PM (3 years ago)
I was able to pinpoint the problem by adding the following code to line 181 in bt_screen_map.java:

try {
if(mapView != null){
mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();
}
} catch (Exception e) {
e.printStackTrace();
}

Apparently the debug SHA1 key was assigned to my release keystore key.

Fixed it by going to file>project structure>app> signing & then build types.
 
GoNorthWest
buzztouch Evangelist
Profile
Posts: 8197
Reg: Jun 24, 2011
Oro Valley, AZ
1,000,000
like
08/11/16 10:49 PM (3 years ago)
Great stuff, Louie! I used this to debug the exact same problem, and it worked like a charm. Thanks!

Mark
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.