简体   繁体   中英

Google maps API: App keeps crashing when I try to use the map

So I am currently trying to develop an app that needs a google map API. Whenever I try to load the map, the app I implemented my map as a subclass of the fragment class:

package io.github.danisharsalan.meetupmarker;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.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.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapActivityFragment extends Fragment {

MapView mMapView;
private GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_map_activity, container, false);

    mMapView = (MapView) rootView.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume(); //needed to make map display immediately

    try{
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e){
        e.printStackTrace();
    }

    mMapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;

            // For showing a move to my location button
            //googleMap.setMyLocationEnabled(true);

            // For dropping a marker at a point on the Map
            LatLng sydney = new LatLng(-34, 151);
            googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));

            // For zooming automatically to the location of the marker
            CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    });


    return rootView;
}

@Override
public void onResume(){
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause(){
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy(){
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory(){
    super.onLowMemory();
    mMapView.onLowMemory();
}


}'

and here is the xml file, titled fragment_map_activity.xml:

<FrameLayout 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"
tools:context="io.github.danisharsalan.meetupmarker.MapActivityFragment">

<!-- TODO: Update blank fragment layout -->

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

The logcat gives me this problem:

02-25 00:29:10.924 5473-5473/io.github.danisharsalan.meetupmarker E/AndroidRuntime: FATAL EXCEPTION: main Process: io.github.danisharsalan.meetupmarker, PID: 5473 java.lang.RuntimeException: API key not found. Check that is in the element of AndroidManifest.xml at com.google.maps.api.android.lib6.drd.qb(:com.google.android.gms.DynamiteModulesB@11947480:40) at com.google.maps.api.android.lib6.auth.ea(:com.google.android.gms.DynamiteModulesB@11947480:11) at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms.DynamiteModulesB@11947480:112) at com.google.android.gms.maps.internal.CreatorImpl.newMapViewDelegate(:com.google.android.gms.DynamiteModulesB@11947480:30) at com.google.android.gms.maps.internal.h.onTransact(:com.google.android.gms.DynamiteModulesB@11947480:45) at android.os.Binder.transact(Binder.java:499) at com.google.android.gms.internal.zzeu.zza(Unknown Source) at com.google.android.gms.maps.internal.zzf.zza(Unknown Source) at com.google.android.gms.maps.MapView$zzb.zza(Unknown Source) at com.google.android.gms.dynamic.zza.zza(Unknown Source) at com.google.android.gms.dynamic.zza.onCreate(Unknown Source) at com.google.android.gms.maps.MapView.onCreate(Unknown S ource) at io.github.danisharsalan.meetupmarker.MapActivityFragment.onCreateView(MapActivityFragment.java:33) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700) at android.os .Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

How do I fix my API key problem? Thank you.

EDIT: It turns out that I was simply missing my API key where It was supposed to be referenced. My apologies for creating a low-quality question

Looks like the Google Maps API key wasn't added to the AndroidManifest file. You can grab Google Maps API key from here

Insert the API key as follows just before the closing tag </application >

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM