Android - Google Map API v2 - Unfortunately, appli

2020-05-02 03:12发布

问题:

Hi guys :) I found similar topics here about Force Closing applications in Android when using Google Map API. I tried almost everything, followed tutorials step by step and checked more than twice - everytime something goes wrong and my application crashes instantly. I am not able to run even an example code from Google. I tried on emulator distributed with SDK, Genymotion, my HTC One X - all times the same crash: "Unfortunately, GMapsTest application has stopped". Here's my logcat, maybe there's something I don't see.

Logcat: http://pastebin.com/dXk6X31t AndroidManifest: http://pastebin.com/WTNH7M2p Java code: http://pastebin.com/JwzV1Vry XML Layout: http://pastebin.com/eUX8Hgdy

Thanks for any help!! :D

回答1:

The problem is in the manifest file. Make sure the add the following meta-data tag inside the application tag

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"
        />


回答2:

Full Edit

Fragment Activity class:

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;

public class Main extends FragmentActivity{

private GoogleMap mMap;

UiSettings settings;
MapController mapController;
GeoPoint geopoint;
RectF oval;
Canvas canvas;
int mRadius = 5;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMap();
}

@Override
protected void onResume(){
    super.onResume();
    setUpMap();
}

private void setUpMap(){
    if (mMap != null) {
        return;
    }
    mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView)).getMap();
    if (mMap == null) {
        return;
    }

    mMap.setTrafficEnabled(true);
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

}

}

Layout file activity_maps:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff" >

    <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/myMapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
     />

</RelativeLayout>

Finally to manifest file add:

<permission
     android:name="your_package.permission.MAPS_RECEIVE"
     android:protectionLevel="signature"/>

<uses-permission android:name="your_package.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" android:debuggable="false">

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


    <activity.....
    </activity>
</application>

Finally right click into your project --> Android --> Add google-play-services_lic to your project as reference. This codes copied from a published project. So this is working for me.