CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
03/30/18 08:12 PM (6 years ago)

Android location map fix in 5 seconds

The reason its crashing is because of line 196 which is: SupportMapFragment mapFragment = getMapFragment(); ---------------------- When debugging android apps it will show you this in logcat. When apps crash it will always say "Fatal Exception". Right under that it has the error. The other errors under that don't matter. It says there is a null pointer exception. Anytime you see null, that means the object is empty and can't be used. The error looks like this: The last line of the posted error actually is underlined blue, which when you click on it will take you straight to line of the error. FATAL EXCEPTION: main Process: com.Mannn, PID: 2768 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at com.Mannn.ui.bt_screens.BT_screen_map.onCreateView(BT_screen_map.java:196) -------------------------------------------------------------- The 5 second fix is to open bt_screen_map.xml and replace the name attribute : android:name="com.google.android.gms.maps.MapFragment" with: android:name="com.google.android.gms.maps.SupportMapFragment" --------------------------------------------------------------- Just by looking at the line of code where the error occurs or the error tells you, you need a SupportMapFragment. What really helped is there is a comment above that line of code saying it's trying to get mapView from the layout, so I thought there must be some issue with layout. So if there is another error it could be there's a support version of it. Check different phones with different api versions to make sure it doesn't crash. I've honestly only tested it on Oreo.
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/30/18 08:28 PM (6 years ago)
I also found xml code by looking at the android documentation: https://developers.google.com/maps/documentation/android-api/start For devices before I think its marshmallow or lollipop Soon Google is supposed to make it where apps are only certain api levels but while these older apis are still relevant: on the line (196) that causes the original error which is: mapFragment = getMapFragment(); change it to this: mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapView); in the onMapReady function change : if (showUserLocation.equals("1")) { to: if (showUserLocation.equals("1") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //( I think it should really be Marshmallow or above for requesting permissions instead lollipop, havent double checked.) ------------- there are some more bugs with older devices with it not getting coordinates so it doesnt zoom. I have a fix but dont have time to mess with more of it right now. the zoom to location button on map works though so kind of don't need it.
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
03/31/18 02:13 AM (6 years ago)
where do you put the second lot of changes dude, im running a kitkat tablet, changed the first lot in the map xml part but dont know where the second lot is sean
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
03/31/18 02:33 AM (6 years ago)
ps tried the first fix on my sony experia phone and didnt work Sean
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/31/18 06:31 AM (6 years ago)
There are so many different xperia phones. What API level it's using is what's important.Have changed xml like in first post? After that change code where you get first error in java. BT_screen_map.java
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/31/18 06:35 AM (6 years ago)
Use ctrl-f or cmd-f depending on what type of computer you have, to search for java code . The first java code is the line it crashed at first 196, search for "onMapReady" to search for second place, bottom of that method
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
03/31/18 10:02 AM (6 years ago)
tried few things, but i need the maps to work on all devices like the old android did, its no use if it just works on some api devices, i will use your button plus till someone gets round to fixing it cheers sean
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/31/18 11:57 AM (6 years ago)
It does for me. I'm saying I need to know what API versions to know how to fix it. You might have misunderstood my instructions or maybe I'm missing something. I have it working on Oreo and KitKat. If it's lollipop there might be issues haven't tested that api. After marshmallow just doing the xml fix will fix it. The java code fixes the lower level stuff. To get the best help post the errors and the Android api level of the device you are having issues. I can make anything work on any API level. It tells you the API level when you are about to run it. It's also in the about phone section under settings.or tell me the exact model of the phone so I can look it up
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/31/18 01:08 PM (6 years ago)
Made it easier. Just copy and paste the code. You will need to change the package name at the top of the java file. To get the right package name at the top save it before you copy, copy only code under it, or open another java file in the bt_screens folder and copy that. If there are any errors post them and the device model or api level that you were testing on where it crashed. It's working on newer and older apis for me. https://www.dropbox.com/s/61xy59x9j70mql4/bt_android_map_fix.zip?dl=0
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
04/01/18 01:30 AM (6 years ago)
its a xperiea spc5303 compileSdkVersion 25 buildToolsVersion '26.0.2' defaultConfig { applicationId "com.chorleyresidential" minSdkVersion 14 targetSdkVersion 21 versionCode 5 versionName '1.0' tries copying your code , but just came up with loads of errors cheers sean
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
04/07/18 05:09 PM (6 years ago)
Can you send a screenshot of android studio and the errors. pm, email or however you want
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
04/08/18 01:00 AM (6 years ago)
can i send you my project, see if you can see what im doing wrong . Thanks sean dont know id this helps, i changed it back used your new code in the xml file, updated app and it crashes when you open map, the below was in in red, 04-09 19:36:42.560 15819-15819/com.chorleyresidential E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.chorleyresidential.ui.bt_screens.BT_screen_map.onCreateView(BT_screen_map.java:196) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298) at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136) at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.main(ActivityThread.java:5225) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method)
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
04/23/18 11:43 AM (6 years ago)
Yes sorry busy with a lot of exams in school
 
snowman7040
Lost but trying
Profile
Posts: 83
Reg: Jul 03, 2013
lawton
5,430
like
02/08/19 07:41 AM (5 years ago)
I know it's a little old but it work to keep it from crashing but maps and pins not load android app studio 3.3.1
 

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.