There was a need to integrate a google map into the project, put it on the google MapFragment guide, hadn’t had a deal with google maps before, checked yesterday on the phone (Samsung Galaxy S4) - the fragment opens and displays the map correctly. The GoogleMaps API on google is activated, the Api key is registered in the manifest. An activity in which fragments are launched is inherited from AppCompatActivity. When you try to go to the fragment with a map on the emulator built into the studio (Nexus 4 21 API (with GoogleAPIs) and Nexus 4 23 API (with GoogleAPIs), the application crashes.

Question: What can be done to run the map fragment correctly and display the maps on the emulator?

I enclose the error of the error, the complete code of the fragment, and partly the layout, the manifest and Build.gradle.

Contact error :

12-07 11:15:09.290 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.290 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.290 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.290 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.290 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.290 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.300 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.300 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.300 2318-2318/xx.xxx.xxx W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8185480 12-07 11:15:09.320 2318-2318/xx.xxx.xxx D/AndroidRuntime: Shutting down VM 12-07 11:15:09.320 2318-2318/xx.xxx.xxx E/ACRA: ACRA caught a NullPointerException for xx.xxx.xxx java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at xx.xxx.xxx.courier.OrdersFragmentMap.onCreateView(OrdersFragmentMap.java:45) at android.app.Fragment.performCreateView(Fragment.java:2220) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) at android.app.BackStackRecord.run(BackStackRecord.java:793) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482) 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:5417) 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) 

Fragment code with maps :

 package xx.xxx.xxx.courier; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import xx.xxx.xxx.R; public class OrdersFragmentMap extends Fragment implements OnMapReadyCallback { public static OrdersFragmentMap newInstance() { return new OrdersFragmentMap(); } public OrdersFragmentMap() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.courier_fragment_map, container, false); MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); return view; } @Override public void onMapReady(GoogleMap googleMap) { LatLng sydney = new LatLng(-33.867, 151.506); googleMap.setMyLocationEnabled(true); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); googleMap.addMarker(new MarkerOptions().title("Sydney").snippet( "The most populous city in Australia.").position(sydney)); } @Override public void onResume() { super.onResume(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onLowMemory() { super.onLowMemory(); } } 

MapFragment containing layout (fragment):

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <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="xx.xxx.xxx.CourierActivity" android:name="com.google.android.gms.maps.MapFragment"/> <!-- other elements --> </RelativeLayout> 

Manifest (fragment):

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xx.xxx.xxx" > <permission android:name="com.example.demomap.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.demomap.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <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_GSRVICES" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- To using maps --> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <application android:name=".xxx" android:allowBackup="true" android:icon="@mipmap/ic_launcher_pink" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <service android:name=".service.ServerService" /> <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSy*****************VJifvg4M4"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <!-- other elements --> </application> </manifest> 

Build.gradle (fpagment):

 dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'com.android.support:support-v4:23.1.1' compile 'com.nbsp:library:1.01' compile 'info.hoang8f:android-segmented:1.0.6' compile 'ch.acra:acra:4.7.0-RC.3' compile 'com.google.android.gms:play-services-maps:8.3.0' compile 'com.google.android.gms:play-services-ads:8.3.0' compile 'com.google.android.gms:play-services-auth:8.3.0' compile 'com.google.android.gms:play-services-gcm:8.3.0' compile 'com.google.android.gms:play-services:8.3.0' } 
  • Gaps on the emulator stand? - Android Android
  • Those. On a real device, this code works, but does it crash on the emulator? - BORSHEVIK
  • @AndroidAndroid Installed on the emulator with 23 API com.android.vending.apk (v6.0.0) and com.google.android.gms.apk (v8.3.01), now before the departure of the emulator writes: "unfortunately google services has stopped", the same thing he writes when starting the store - rockon
  • @BORSHEVIK exactly. - rockon
  • one
    and this line seems to have an extra compile 'com.google.android.gms: play-services: 8.3.0' - Android Android

1 answer 1

Solved the problem as follows. First, I changed the code of the fragment class so that the application does not crash on API versions 21 and higher:

 public class OrdersFragmentMap extends Fragment implements OnMapReadyCallback, LoaderManager.LoaderCallbacks<Cursor> { ArrayList<Order> freeOrders = new ArrayList<>(); ArrayList<Order> myOrders = new ArrayList<>(); private FragmentActivity myContext; public static OrdersFragmentMap newInstance() { return new OrdersFragmentMap(); } public OrdersFragmentMap() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.courier_fragment_map, container, false); FragmentManager fm = myContext.getSupportFragmentManager(); SupportMapFragment mapFragment = SupportMapFragment.newInstance(); fm.beginTransaction().replace(R.id.map, mapFragment).commit(); mapFragment.getMapAsync(this); return view; } @Override public void onMapReady(GoogleMap googleMap) { LatLng sydney = new LatLng(-33.867, 151.506); googleMap.setMyLocationEnabled(true); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); googleMap.addMarker(new MarkerOptions().title("Sydney").snippet( "The most populous city in Australia.").position(sydney)); } @Override public void onResume() { super.onResume(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onLowMemory() { super.onLowMemory(); } @Override public void onAttach(Activity activity) { myContext=(FragmentActivity) activity; super.onAttach(activity); 

The onAttach () method is obsolete and I use it temporarily only to run android.support.v4.app.FragmentManager in the AppCompatActivity.

Secondly, I created a MapActivity template. To do this, right-click in the project root and select new> Google> GoogleMapsActivity. Of the three files created, I left only res / values ​​/ google_maps_api.xml (debug), copied from it a link to generate the key that is in the comment, followed it in the browser, created the key and it all worked. The strangest thing is that before this he introduced the same fingerprint and package name. Such are the cases.

And on the Galaxy S4 API it worked, apparently, bypassing Google protection, since there were no requests in the developer console while the application was running.