2014-02-17 192 views
1

嘗試將地圖添加到Android應用程序中,我已經創建了API密鑰並啓用了Google地圖Android V2;我在清單文件中添加了權限,Google Play服務版本和API密鑰。我還在項目中添加了Google Play服務庫。谷歌地圖android API V2失敗

但是,當我啓動應用程序停止時,我不知道爲什麼,我認爲這是與權限有關的事情,但只是爲了確保我複製/過去Google開發人員建議的權限向我們展示如何添加地圖的網站。但就像我說的,我不確定這是否是真正的交易。 這裏是我的代碼:

清單文件:

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

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


<!-- ============ PERMISSIONS: ============ --> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
<!-- The following two permissions are not required to use 
    Google Maps Android API v2, but are recommended. --> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

<!-- ============ END PERMISSIONS =============== --> 
      <!-- this will notify the user that the device needs OpenGL ES version 2 to display the map (as recomanded from the google developpers website)--> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Black.NoTitleBar" > 
<!-- ==== google play services version: ==== --> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name="usthb.appusthb.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 Map Android API V2 === ==== === API key: --> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyCfqonheoI9OG_REcsnl7-xgmpiFwu692I"/> 

    </application> 

</manifest> 

MainLayout:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:name="com.google.android.gms.maps.MapFragment"/> 

MainActivity:

登錄:

02-17 10:50:11.928: E/AndroidRuntime(1356): FATAL EXCEPTION: main 
02-17 10:50:11.928: E/AndroidRuntime(1356): Process: usthb.appusthb, PID: 1356 
02-17 10:50:11.928: E/AndroidRuntime(1356): java.lang.RuntimeException: Unable to start activity ComponentInfo{usthb.appusthb/usthb.appusthb.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.os.Handler.dispatchMessage(Handler.java:102) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.os.Looper.loop(Looper.java:136) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at java.lang.reflect.Method.invoke(Method.java:515) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at dalvik.system.NativeStart.main(Native Method) 
02-17 10:50:11.928: E/AndroidRuntime(1356): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.Activity.setContentView(Activity.java:1929) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at usthb.appusthb.MainActivity.onCreate(MainActivity.java:12) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.Activity.performCreate(Activity.java:5231) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  ... 11 more 
02-17 10:50:11.928: E/AndroidRuntime(1356): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior: 
02-17 10:50:11.928: E/AndroidRuntime(1356): <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at maps.af.ch.a(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at maps.af.ay.a(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at maps.af.ay.a(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at maps.af.al.a(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at maps.af.bg.a(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at maps.af.bf.a(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at ebt.onTransact(SourceFile:107) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.os.Binder.transact(Binder.java:361) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.google.android.gms.dynamic.a$4.b(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.google.android.gms.dynamic.a.a(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.google.android.gms.dynamic.a.onCreateView(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.Fragment.performCreateView(Fragment.java:1700) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.app.Activity.onCreateView(Activity.java:4786) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) 
02-17 10:50:11.928: E/AndroidRuntime(1356):  ... 20 more 
+0

您確定堆棧跟蹤是由此代碼生成的嗎? – CommonsWare

+0

是的它是由這個代碼產生的 –

回答

1

更改此

android:name="com.google.android.gms.maps.MapFragment"/> 

android:name="com.google.android.gms.maps.SupportMapFragment"/> 

因爲你分鐘SDK是11.所以你需要使用SupportMapFragment。 12和以上使用MapFragment

+0

還是一樣的錯誤! –

+1

@ Tha.Poox確定代碼和堆棧跟蹤屬於Map應用程序?因爲我確實看到清單中的權限,但巡迴堆棧跟蹤表明不同,並確保您正確引用谷歌播放服務? – Raghunandan

+0

是的人,你知道嗎,我只是創建另一個項目,我會刪除這一個... –