1

我正在關注this教程。這裏是我的所有相關文件: activity_main.xml中谷歌地圖v2簡單應用程序不工作

<?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> 

MainActivity.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> 

我完成了教程,直至標題設置標記 。但是,我收到以下錯誤。

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

如果我寫我的清單文件中的以下行:

<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 

的是Android在開玩笑嗎?爲什麼代碼崩潰?我該怎麼做才能運行那該死的簡單代碼?爲什麼它在教程中而不是在我的機器上工作。它讓我很煩惱。

(順便說一句,我使用的是Windows 7與真實設備的Nexus 7和我複製一個version.xml也試過以下元數據

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

,再沒有工作。最後,當然我搜索了谷歌和谷歌看着流行的教程)請幫助我。

+0

你有沒有'元data'你的'AndroidManifest.xml'中的東西? – zapl

+0

你還需要一個鍵使用谷歌地圖,該鍵也必須在你的清單 – tyczj

+0

沒有zapl,我只有一個元數據字段,並試圖用3個不同的字符串。我得到了一個關鍵tyczj,並嘗試過但沒有工作。我在問題中講過了。 –

回答

7

你需要有兩個meta-data領域。一般來說,谷歌播放服務需要一個,第二個使用地圖。

例如,從示例應用程序代碼段,你應該在[android-sdk]/extras/google/google_play_services/samples/maps/AndroidManifest.xml

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

感謝朋友,我的錯:)遺憾的是,我不得不選擇一個作爲接受的答案。我選擇了答案和簡短的解釋。 –

+0

@ integer/google_play_services_version評估的是什麼? –

+1

@IgorGanapolsky目前4132500.它在'[android-sdk]/extras/google/google_play_services/libproject/google-play-services_lib/res/values/version.xml' – zapl

3

你需要

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

    ... 
</application> 
+0

@ integer/google_play_services_version在哪裏定義? –

+1

在谷歌播放服務庫 – tyczj

0

你需要做給外部存儲權限既元數據:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <application 
    <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyCm5xJy0AwN4TfXM6ccpfj2GhqZneFCyiM" /> 
    </application>