我无法使用基本的Google Maps V2

时间:2015-01-27 07:30:52

标签: java android google-maps

回答。

我按照此处的说明http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/ 而另一个基本上说同样的东西,但我无法让它工作。 我(希望)正确导入了google-play-services-lib,并为我的程序提取了SHA1密钥。

这是基本程序的代码 MainActivity.java

package com.km.googlemapsv2;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap;

public class MainActivity extends ActionBarActivity {

// Google Map
private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        // Loading map
        initializeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initializeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

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

}

activity_main.xml中

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.km.googlemapsv2.MainActivity" >

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

GoogleMapsV2 Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.km.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >

<permission
    android:name="com.km.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.km.googlemapsv2.permission.MAPS_RECEIVE" />

<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<!-- Google Maps API Key -->
<meta-data
 android:name="com.google.android.maps.v2.API_KEY"
 android:value="AIzaSyBf-0hUDrDTK-NOveZrnT8wec5TTTLqjAw" />

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

</manifest>

这是logcat

01-27 16:30:10.492: I/art(10015): Late-enabling -Xcheck:jni
01-27 16:30:11.148: D/AndroidRuntime(10015): Shutting down VM
01-27 16:30:11.160: E/AndroidRuntime(10015): FATAL EXCEPTION: main
01-27 16:30:11.160: E/AndroidRuntime(10015): Process: com.km.googlemapsv2, PID: 10015
01-27 16:30:11.160: E/AndroidRuntime(10015): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.km.googlemapsv2/com.km.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.os.Looper.loop(Looper.java:135)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.ActivityThread.main(ActivityThread.java:5221)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at java.lang.reflect.Method.invoke(Native Method)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at java.lang.reflect.Method.invoke(Method.java:372)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-27 16:30:11.160: E/AndroidRuntime(10015): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.km.googlemapsv2.MainActivity.onCreate(MainActivity.java:19)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.Activity.performCreate(Activity.java:5933)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-27 16:30:11.160: E/AndroidRuntime(10015):    ... 10 more
01-27 16:30:11.160: E/AndroidRuntime(10015): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.common.GooglePlayServicesUtil.D(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.maps.internal.x.T(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.maps.internal.x.S(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.maps.MapFragment$b.nO(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.dynamic.a.a(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2115)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.app.Activity.onCreateView(Activity.java:5282)
01-27 16:30:11.160: E/AndroidRuntime(10015):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
01-27 16:30:11.160: E/AndroidRuntime(10015):    ... 20 more
01-27 16:30:19.074: I/Process(10015): Sending signal. PID: 10015 SIG: 9

基本上我只需要移动

<!-- Google Maps API Key -->
<meta-data
 android:name="com.google.android.maps.v2.API_KEY"
 android:value="AIzaSyBf-0hUDrDTK-NOveZrnT8wec5TTTLqjAw" />

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

进入应用程序标签。感谢用户名arpit获得答案!

2 个答案:

答案 0 :(得分:0)

在清单中添加此项并清理项目并再次运行

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

答案 1 :(得分:0)

在您的AndroidManifest文件中,在应用程序代码中提供 Google Map API密钥元数据 Google Play服务元数据

   <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.km.googlemapsv2"
    android:versionCode="1"
    android:versionName="1.0" >

    ...

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <!-- Google Maps API Key -->
    <meta-data
         android:name="com.google.android.maps.v2.API_KEY"
         android:value="AIzaSyBf-0hUDrDTK-NOveZrnT8wec5TTTLqjAw" />

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

    </application>

    </manifest>