2013-04-05 67 views
0

我想實現谷歌地圖。但它總是會出錯。谷歌地圖在Android API v2顯示錯誤

我的動態

package com.alex.googlemaps; 

import android.os.Build; 
import android.os.Bundle; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.view.Menu; 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class MainActivity extends Activity { 

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

     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
     final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097); 
     Marker ciu = mMap.addMarker(new MarkerOptions() 
            .position(CIU).title("My Office")); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

我的XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 



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

</RelativeLayout> 

清單文件

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

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

    <permission 
     android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" > 
    </permission> 

    <uses-permission android:name="com.alex.googlemaps.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <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-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.alex.googlemaps.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="AIzaSyD-zPbxq6kH0cjw-Axfey-WZ3cmlXqf2LE" /> 
    </application> 

</manifest> 

而且logcat中顯示爲

04-05 05:44:02.598: E/Trace(1057): error opening trace file: No such file or directory (2) 
04-05 05:44:03.038: W/GooglePlayServicesUtil(1057): Google Play services out of date. Requires 3025100 but found 2012110 
04-05 05:44:03.058: W/GooglePlayServicesUtil(1057): Google Play services out of date. Requires 3025100 but found 2012110 
04-05 05:44:03.148: W/GooglePlayServicesUtil(1057): Google Play services out of date. Requires 3025100 but found 2012110 
04-05 05:44:03.188: W/GooglePlayServicesUtil(1057): Google Play services out of date. Requires 3025100 but found 2012110 
04-05 05:44:03.198: W/GooglePlayServicesUtil(1057): Google Play services out of date. Requires 3025100 but found 2012110 
04-05 05:44:03.258: W/GooglePlayServicesUtil(1057): Google Play services out of date. Requires 3025100 but found 2012110 
04-05 05:44:03.258: D/AndroidRuntime(1057): Shutting down VM 
04-05 05:44:03.268: W/dalvikvm(1057): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
04-05 05:44:03.278: E/AndroidRuntime(1057): FATAL EXCEPTION: main 
04-05 05:44:03.278: E/AndroidRuntime(1057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alex.googlemaps/com.alex.googlemaps.MainActivity}: java.lang.NullPointerException 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.os.Looper.loop(Looper.java:137) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at dalvik.system.NativeStart.main(Native Method) 
04-05 05:44:03.278: E/AndroidRuntime(1057): Caused by: java.lang.NullPointerException 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at com.alex.googlemaps.MainActivity.onCreate(MainActivity.java:24) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.app.Activity.performCreate(Activity.java:5104) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
04-05 05:44:03.278: E/AndroidRuntime(1057):  ... 11 more 
04-05 05:44:03.378: D/dalvikvm(1057): GC_CONCURRENT freed 257K, 14% free 2545K/2948K, paused 16ms+3ms, total 114ms 
04-05 05:44:06.338: I/Process(1057): Sending signal. PID: 1057 SIG: 9 

我google了很多,但所有的結果最後都是錯誤的。有人請幫我解決這個問題。

回答

1

我懷疑你的問題是谷歌播放APK。在你的logcat中,它表明它已經過時並且在這裏:http://developer.android.com/reference/com/google/android/gms/maps/MapFragment.html它聲明如果谷歌播放不可用MapFragment.getMap()將返回null,這與你得到的錯誤是一致的。

希望這會有所幫助!

+0

請給我一個示例 – 2013-04-05 06:01:57

+0

恐怕我不能 - 如果你更新你的谷歌播放服務,它會解決問題或不會 - 你的代碼本身沒有任何問題。你可以在這裏找到如何更新谷歌播放服務:http://developer.android.com/google/play-services/setup.html – GHC 2013-04-05 06:05:29

+0

我已經有最新版本。無論如何感謝您找出問題所在。 – 2013-04-05 06:12:07

2

發現憑身份證視圖後嘗試從地圖片段獲取地圖爲例說明如下:

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

     MapFragment mpFragment= ((MapFragment) getFragmentManager().findFragmentById(R.id.map)); 
     mMap=mpFragment.getMap(); 
     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
     final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097); 
     Marker ciu = mMap.addMarker(new MarkerOptions() 
            .position(CIU).title("My Office")); 
    } 
+0

這不看任何不同於上面貼的代碼(不是讓該片段到一個單獨的變量等。難道我失去了一些東西? – GHC 2013-04-05 05:58:18

+0

不,它並沒有解決問題。 – 2013-04-05 05:59:04

+1

您是否試圖在模擬器或手機上運行? – 2013-04-05 06:00:59