Android谷歌地图API(v2)显示当前位置获取“很遗憾应用程序已停止”错误

3
我是一名Android初学者,我正在跟随教程学习如何在Android上使用Google地图展示当前位置。以下是代码:
MainActivity.java
package com.example.android_gps_location_detection;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity {
GoogleMap map;
LocationManager lm;

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

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapView);
    map = mapFragment.getMap();

    map.setMyLocationEnabled(true);

    LocationListener ll = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {

            Toast.makeText(getApplicationContext(),
                    location.getLatitude() + " " + location.getLongitude(),
                    Toast.LENGTH_LONG).show();

            map.addMarker(new MarkerOptions()
                    .position(
                            new LatLng(location.getLatitude(), location
                                    .getLongitude()))
                    .title("my position")
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

            map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    location.getLatitude(), location.getLongitude()), 15.0f));

        }
    };
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

}

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment  
      android:id="@+id/mapView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

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

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

    <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBqwF4D-JhpA32_OL8Bw0dKDqc7Jm0CWHQ"/>

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

    <activity
        android:name="com.example.android_gps_location_detection.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>
</application>

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

<permission
    android:name="com.ram.mapsv2whereami.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.ram.mapsv2whereami.permission.MAPS_RECEIVE" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

</manifest>

我遇到了这个错误:map.setMyLocationEnabled(true);这一行代码报了一个空指针异常,我怀疑是getMap()方法返回了null。我进行了一些研究,发现有人建议使用SupportMapFragment。Google Play服务过期了,需要3225100版本,但我只找到了3158130版本。

项目构建目标为Google API 18平台4.3,我的AVD目标也是Google API 18级别。


看看这个例子:http://wptrafficanalyzer.in/blog/showing-current-location-using-onmylocationchangelistener-in-google-map-android-api-v2/ - Aravin
我按照示例操作,并尝试使用我的安卓手机进行调试,但是出现了相同的错误:“未找到Google Play服务资源。请检查您的项目配置以确保资源已包含在内。” - user236501
你在Eclipse中安装了Google Play服务吗? - Aravin
已安装。地图只是从未显示出来。 - user236501
你是否在属性中检查了Google Play服务项目。如果没有,请转到项目->右键单击->属性->Android->添加->Google Play服务,然后转到Java构建路径,按顺序检查所有内容并导入。 - Aravin
2个回答

1
我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我也遇到了同样的问题。在我的情况下,它显示了2个错误:

The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

Failed to load map. Error contacting google servers

我通过以下方式解决了这个问题:
  1. 验证您的API密钥。这并没有解决我的问题。
  2. 确保项目正确导入。这也没有解决问题。
  3. 我的API密钥有效,库已正确导入,所以我重新下载了Google Play服务库。
  4. 清理并重新构建所有项目。不行。
  5. 卸载并安装应用程序(不仅是重新安装)。这解决了我的问题!

编辑:但是它也不起作用,因为您正在模拟器中运行应用程序。

AVD上不起作用,因为即使最新的AVD也不支持最新的Google Play Services SDK(这是一个错误)。

我建议您通过下载旧版SDK并将其复制到相应文件夹中来降级Google Play Services SDK。

另外,如果创建一个线程,在初始化Google Map之前不断检查getMap()是否为null,它会在特殊情况下修复一些错误。

编辑:顺便说一句,在activity_main.xml中,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment  
      android:id="@+id/mapView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

我不知道为什么这个xml文件的<?xml version="1.0" encoding="utf-8"?>在最后一行。
XML声明不应该在XML的第一行吗?

@AllenChan 不用谢!如果你把这个标记为答案(按下投票按钮下面的复选标记),你将更受欢迎并获得声望。 - minmaxavg

1
问题出在您的XML文件activity_main.xml中:
<fragment  
  android:id="@+id/mapView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="com.google.android.gms.maps.SupportMapFragment"
  />

你需要使用class而不是android:name,就像这样:

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

编辑:

抱歉,应该只是class:,而不是android:class。


嗨,我遇到了这个错误:在android包中找不到属性“class”的资源标识符。 - user236501
尝试移除android命名空间,只使用"class"。我已经编辑了我的答案。 - lifeson106
lifeson106 指出了你的代码和我的代码之间唯一的区别。最近有一个 Google Play 服务更新(版本 10)。如果你还没有更新,请尝试进行更新。 - TrueCoke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接