0

我正在尝试实施谷歌地图。但它总是会出错。

我的活动

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

我用谷歌搜索了很多,但所有结果最后都给出了错误。请任何人帮助我解决这个问题。

4

3 回答 3

2

通过 Id 找到视图后,尝试从 Map 片段中获取地图实例,如下所示:

@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"));
    }
于 2013-04-05T05:54:18.427 回答
1

我怀疑您的问题是 Google Play APK。在您的 logcat 中,它声明它已过时,并且在这里:http: //developer.android.com/reference/com/google/android/gms/maps/MapFragment.html它声明如果谷歌播放不可用 MapFragment.getMap()将返回 null,这与您得到的错误一致。

希望这可以帮助!

于 2013-04-05T05:57:35.033 回答
0

即使这已被标记为已解决,我仍然愿意为我的案例的解决方案发布一些信息。

请参考答案如何查看 Google Play 服务版本?

于 2013-10-23T06:50:44.040 回答