Mohamed Elbalshy Mohamed Elbalshy - 21 days ago 8
Java Question

Add Google Map to my android app

the project build and run successfully but when i launch the app it crashes says unfortunately your app stopped.

can anyone help me please??

What is the wrong with this code?

here is the MainActivity.java

package app.elbalshy.mohamed.myapplication_map;

import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
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.SupportMapFragment;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
googleServicesAvilable();
Toast.makeText(this, "Perfect", Toast.LENGTH_LONG).show();
initMap();





}

private void initMap() {
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
}

public boolean googleServicesAvilable()
{
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int isAvilable = api.isGooglePlayServicesAvailable(this);
if(isAvilable== ConnectionResult.SUCCESS)
{
Toast.makeText(this, "SUCCESSFULLY", Toast.LENGTH_LONG).show();
return true;
}
else if(api.isUserResolvableError(isAvilable))
{
Toast.makeText(this, "ResolvableError", Toast.LENGTH_LONG).show();
Dialog dialog = api.getErrorDialog(this, isAvilable,0);
dialog.show();
}
else
{
Toast.makeText(this, "Cant't connect to Play Services.", Toast.LENGTH_LONG).show();
}
return false;
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;


}
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.elbalshy.mohamed.myapplication_map">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<permission android:name="app.elbalshy.mohamed.myapplication_map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="app.elbalshy.mohamed.myapplication_map.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />



<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.maps.v2.API_KEY"
android:value="AIzaSyBT6CZPIS5XY37b0r0D-6OQEPFb4ebWKtA"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

</application>

</manifest>


and this is my Logcat

11-12 06:30:37.071 11349-11349/? D/dalvikvm: Late-enabling CheckJNI
11-12 06:30:40.124 11349-11349/app.elbalshy.mohamed.myapplication_map D/AndroidRuntime: Shutting down VM
11-12 06:30:40.124 11349-11349/app.elbalshy.mohamed.myapplication_map W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41e64960)
11-12 06:30:40.194 11349-11349/app.elbalshy.mohamed.myapplication_map E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: /data/app/app.elbalshy.mohamed.myapplication_map-4.apk
at android.app.ActivityThread.installProvider(ActivityThread.java:5022)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4631)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4571)
at android.app.ActivityThread.access$1400(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: /data/app/app.elbalshy.mohamed.myapplication_map-4.apk
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.ActivityThread.installProvider(ActivityThread.java:5007)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4631) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4571) 
at android.app.ActivityThread.access$1400(ActivityThread.java:157) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:176) 
at android.app.ActivityThread.main(ActivityThread.java:5317) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
at dalvik.system.NativeStart.main(Native Method) 


my build.gradle file

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "app.elbalshy.mohamed.myapplication_map"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-maps:9.8.0'
}

Answer

downgrade google play-service dependency from

compile 'com.google.android.gms:play-services-maps:9.8.0'

to

compile 'com.google.android.gms:play-services-maps:9.4.0'
compile 'com.google.android.gms:play-services-location:9.4.0'
Comments