Search code examples
javaandroidgoogle-mapsgoogle-maps-api-2

Google map layout called from button press crash


I'm trying to implement Google Maps API in an Android application. This works just fine when the google map is in an app alone But when I try to open the layout containing the map via a button tap from another layout, it crashes at the line: setContentView(R.layout.activity_maps); in the mapsactivity.java.

here is the function used to call the map layout:

public void runSSH(View v) {

    Intent intent = new Intent(this, MapsActivity.class);
    startActivity(intent);
}

The mapsactivity.java was auto generated by AndroidStudio, but I will still put it here:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


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

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

And here is the Activitymaps.xml (auto-generated as well):

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.hosenbocus.azhar.droner.MapsActivity" />

I did add the API key in the manifest and in the google_maps_api.xml

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MYKEY-AIzaSyCf0-a3jUzVrL4PJwCo1q6W60YTxu-U7F0" />
<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />




<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">
    AIzaSyCf0-a3jUzVrL4PJwCo1q6W60YTxu-U7F0
</string>

Here is the trace log:

    03-10 14:47:20.937 22341-22341/com.hosenbocus.azhar.droner E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hosenbocus.azhar.droner/com.hosenbocus.azhar.droner.MapsActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
    at android.app.ActivityThread.access$600(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    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)
    Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)
    at android.app.Activity.setContentView(Activity.java:1895)
    at com.hosenbocus.azhar.droner.MapsActivity.onCreate(MapsActivity.java:20)
    at android.app.Activity.performCreate(Activity.java:5133)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
    at android.app.ActivityThread.access$600(ActivityThread.java:150) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    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) 
    Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.geo.API_KEY in the <application> element of AndroidManifest.xml
    at maps.db.r.a(Unknown Source)
    at maps.cz.c.a(Unknown Source)
    at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
    at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(Unknown Source)
    at com.google.android.gms.maps.internal.d$a.onTransact(:com.google.android.gms.alldynamite:62)
    at android.os.Binder.transact(Binder.java:347)
    at com.google.android.gms.maps.internal.ah.b(:com.google.android.gms:179)
    at com.google.android.gms.maps.internal.CreatorImpl.b(:com.google.android.gms:100)
    at com.google.android.gms.maps.internal.ag.onTransact(:com.google.android.gms:62)
    at android.os.Binder.transact(Binder.java:347)
    at com.google.android.gms.maps.internal.zzc$zza$zza.zzs(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment$zzb.zzzW(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source)
    at com.google.android.gms.dynamic.zza.zza(Unknown Source)
    at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
    at android.support.v4.app.Fragment.onInflate(Fragment.java:1142)
    at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2287)
    at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
    at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
    at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274) 
    at android.app.Activity.setContentView(Activity.java:1895) 
    at com.hosenbocus.azhar.droner.MapsActivity.onCreate(MapsActivity.java:20) 
    at android.app.Activity.performCreate(Activity.java:5133) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
    at android.app.ActivityThread.access$600(ActivityThread.java:150) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    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) 

Please help me, I'm stuck on this for 2 days already now. I'm a beginner to android development, there might be something obvious that I missed.

Thanks in advance


Solution

  • Problem is :

    The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.geo.API_KEY in the <application> element of AndroidManifest.xml
    

    Instant of

    <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="MYKEY-AIzaSyCf0-a3jUzVrL4PJwCo1q6W60YTxu-U7F0" />
    <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
    

    use only

    <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />