2015-05-09 56 views
1

請找到下面的代碼。谷歌地圖不是在Android應用程序加載,應用程序運行時出現空白屏幕

的Manifest.xml --added所有必需的權限和API密鑰

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

    <permission 
     android:name="com.achra.fma.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 


    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="com.achra.fmap.permission.MAPS_RECEIVE"/> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 



    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <uses-library android:name="com.google.android.maps"/> 

     <activity 
      android:name="com.achra.fmap.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> 
     <activity android:name="com.achra.fmap.Activity_map"/> 
     <activity android:name=".FMap" 
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar"/> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="removed key intentionally" /> 

    </application> 


</manifest> 

map_view.xml --layout文件

<?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_Map.java

package com.achra.fmap; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.widget.Toast; 

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

    public class Activity_map extends Activity { 

    // Google Map 
    private GoogleMap googleMap; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map_view); 

     try { 
      // Loading map 
      initilizeMap(); 

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

    } 

    /** 
    * function to load map. If map is not created it will create it for you 
    * */ 
    private void initilizeMap() { 
     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(); 
     initilizeMap(); 
    } 

    } 

我們可以看到在屏幕上顯示地圖網格,但不顯示地圖。 請繼續指導。

我們正在使用Google Maps API v2。

+0

嘗試改變'MapFragment'到'SupportMapFragment' –

回答

0

看起來像你在你的AndroidManifest.xml中缺少com.google.android.gms.version

如果你看看你的日誌,你可能會看到:

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

嘗試增加它只是下方的API密鑰:

<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="removed key intentionally" /> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
+0

在將「com.google.android.gms.version」添加到Manifest.xml中,我們在「@ integer/google_play_services_version」行上得到了以下錯誤 - >找不到與指定名稱匹配的資源。 我們在工作區中導入了包google-play-services_lib。 – Achra

+0

看看這篇文章:http://stackoverflow.com/questions/19843784/google-play-services-library-update-and-missing-symbol-integer-google-play-serv –

+0

@Achra此答案可能會幫助您太。如果一切都失敗了,請執行Project->幾次清理。 http://stackoverflow.com/questions/19879844/adding-google-play-services-version-to-your-apps-manifest/22648826#22648826 –

相關問題