java - 谷歌地图 v2 简单应用程序不工作

标签 java android google-maps android-manifest google-maps-api-2

我正在关注 this教程。这是我所有的相关文件: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

主 Activity .java

package org.barisakkurt.googlemapsv2;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {

    // Google Map
    private GoogleMap googleMap;

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

        try {
            // Loading map
            initilizeMap();
        } catch (Exception e) {

        }
    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {

            googleMap = ((SupportMapFragment)  getSupportFragmentManager().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();
        initilizeMap();
    }

}

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.barisakkurt.googlemapsv2.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>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAC7y4bP-MjeJljm6Fr9Og1FKxNe9ZiFkY" />
    </application>

</manifest>

我完成了教程,直到标题为 Placing a Marker。但是我收到以下错误。

Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4030500 but found 0.  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" />

如果我在我的 list 文件中写下以下几行:

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

这次我收到这个错误:

Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

安卓在跟我开玩笑吗?为什么该代码崩溃?我需要做什么才能运行该死的简单代码?为什么它在教程中有效而不是在我的机器上。这让我很恼火。

(顺便说一句,我正在使用带有真实设备 Nexus 7 的 Windows 7,我复制了一个 version.xml 还尝试了以下元数据

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

再次失败。最后,当然我搜索了 SO 并谷歌查看了流行的教程)请帮助我。

最佳答案

您需要有两个元数据 字段。一般情况下,Google Play 服务需要一个,使用 map 需要第二个。

您应该在 [android-sdk]/extras/google/google_play_services/samples/maps/AndroidManifest.xml 中包含的示例应用程序的示例 fragment

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/demo_title"
    android:hardwareAccelerated="true">
    <!-- ** You need to replace the key below with your own key. **
         The example key below will not be accepted because it is not linked to the
         certificate which you will use to sign this application.
         See: https://developers.google.com/maps/documentation/android/start
         for instructions on how to get your own key. -->

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

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

关于java - 谷歌地图 v2 简单应用程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20668496/

相关文章:

java - 嵌入 Java 服务器应用程序的脚本语言

java - 哪个一位整数具有更高的有效位?

java - Android 在 Galaxy Note 上的 Activity (单例)之间传递数据不起作用

javascript - 来自异步加载的外部脚本的警告 : A call to document. write() 被忽略。这是如何固定的?

javascript - 谷歌地图错误 : Uncaught InvalidValueError: setIcon: not a string; and no url property; and no path property

java - JSP 显示用户详细信息根本不起作用

java - 使用 JDK 7 编译的 scala 代码可以在 JVM 8 上运行吗?

java - 在具有相同密度的不同 Android 设备上不同的 throw (滑动)速度

android - 列表项布局 View 显示隐藏

javascript 谷歌地图 API v3 标记和信息窗口