2

我是Android谷歌地圖API的新手,我試圖使用API​​開發一個小應用程序。我使用Google文檔格式作爲參考:https://developers.google.com/maps/documentation/android/,我謹慎行事。當我構建它並將其安裝到我的設備(Android 2.3.6)時,應用程序崩潰。我按照所有說明簽署應用程序並獲取該特定應用程序的API密鑰,並多次嘗試文檔的所有步驟,但沒有獲得任何好的結果。請,有人可以幫助我嗎?我有什麼缺失? 謝謝!從Android開始谷歌地圖API V2

的Manifest.xml

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

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

<permission 
    android:name="com.application.uprm_map.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature"/> 
<uses-permission android:name="com.application.uprm_map.permission.MAPS_RECEIVE"/> 
<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true"/> 
<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"/> 
<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="com.application.uprm_map.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="<my own key>"/> 

</application> 
</manifest> 

activity_main.xml中:

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

package com.application.uprm_map; 

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

public class MainActivity extends Activity { 

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

產生的異常:

03-05 02:29:27.645: D/dalvikvm(32056): GC_EXTERNAL_ALLOC freed 42K, 49% free  2748K/5379K, external 0K/0K, paused 21ms 
03-05 02:29:27.650: D/AndroidRuntime(32056): Shutting down VM 
03-05 02:29:27.650: W/dalvikvm(32056): threadid=1: thread exiting with uncaught exception (group=0x4001e578) 
03-05 02:29:27.655: E/AndroidRuntime(32056): FATAL EXCEPTION: main 
03-05 02:29:27.655: E/AndroidRuntime(32056): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.uprm_map/com.application.uprm_map.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.os.Looper.loop(Looper.java:130) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.app.ActivityThread.main(ActivityThread.java:3691) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at java.lang.reflect.Method.invokeNative(Native Method) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at java.lang.reflect.Method.invoke(Method.java:507) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at dalvik.system.NativeStart.main(Native Method) 
03-05 02:29:27.655: E/AndroidRuntime(32056): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.view.LayoutInflater.inflate(LayoutInflater.java:386) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:234) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.app.Activity.setContentView(Activity.java:1679) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at com.application.uprm_map.MainActivity.onCreate(MainActivity.java:11) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
03-05 02:29:27.655: E/AndroidRuntime(32056): ... 11 more 
03-05 02:29:27.655: E/AndroidRuntime(32056): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.application.uprm_map-1.apk] 
03-05 02:29:27.655: E/AndroidRuntime(32056): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.view.LayoutInflater.createView(LayoutInflater.java:471) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 
03-05 02:29:27.655: E/AndroidRuntime(32056): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) 
03-05 02:29:27.655: E/AndroidRuntime(32056): ... 19 more 
03-05 02:29:29.660: I/dalvikvm(32056): threadid=4: reacting to signal 3 
03-05 02:29:29.665: I/dalvikvm(32056): Wrote stack traces to '/data/anr/traces.txt' 
+0

已更改您所需的android google API版本的屬性@ user2134466 – Danny 2013-03-05 06:41:00

回答

4

首先,因爲您的API級別爲8或更低,所以您必須在佈局文件中使用SupportMapFragment而不是MapFragment

<?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.SupportMapFragment"/> 

第二件事你必須擴展的FragmentActivity代替Activity

public class MainActivity extends FragmentActivity { 

做好了以上的修改,然後運行應用程序。

+0

非常感謝!它的工作! – user2134466 2013-03-05 21:28:40

+0

@GrlsHu如果我遵循這個鏈接,我是否需要導入一些jar文件如果是的話,你可以請名稱的jar文件,並可以提供給我嗎? – 2013-10-14 08:11:04

+0

您需要在項目中添加「android-support-v4」。 – GrIsHu 2013-10-14 08:40:50