【问题标题】:error with google map v2谷歌地图v2错误
【发布时间】:2014-08-18 12:58:21
【问题描述】:

我正在尝试在我的应用上显示或查看谷歌地图。

  1. 我已添加库 google_play_services_lib(没有错误)。
  2. 获取 google maps api 密钥。
  3. 关注http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html#overview_intro中的代码 (没有错误)。
  4. 但是在模拟器中运行应用程序,它停止了。它说“不幸的是 ShowMapActivity 已停止”。

我没有收到任何错误。我认为问题出在 xml 文件中。片段不起作用。我现在该怎么办。请帮助我或使用谷歌地图给我简单的示例项目。我是 android 谷歌地图的初学者,并试图修复它但是......:(

这里是 activity_main.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"
     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> 

MainActivity.java 文件:

    package com.vogella.android.locationapi.maps;

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

    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.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.Marker;
    import com.google.android.gms.maps.model.MarkerOptions;

    public class MainActivity extends Activity {
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /*map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
    .getMap();
    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
    Marker kiel = map.addMarker(new MarkerOptions()
    .position(KIEL)
    .title("Kiel")
    .snippet("Kiel is cool")
    .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.ic_launcher)));

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    */  }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
    }

和 AndroidManifest.xml 文件:

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

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

<permission
    android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

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

<uses-permission  android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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.vogella.android.locationapi.maps.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.AIzaSyADyrVyRG68lvuQ0721D_Iaw0VLaeMjNuA"
        android:value="AIzaSyADyrVyRG68lvuQ0721D_Iaw0VLaeMjNuA" />
</application>

 </manifest> 

【问题讨论】:

标签: java android xml google-maps


【解决方案1】:

我张贴我的地图片段作为参考。您可以添加缺少的属性,看看它是否有效。

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/transparent"
    map:cameraZoom="11" />

【讨论】:

    【解决方案2】:

    用这个替换 AndroidManifest.xml 文件中的 meta-data

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

    也添加这个meta-data

    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

    希望这能解决您的问题。

    【讨论】:

    • 正如@Kaushik所说,您需要使用真实设备来正确测试它
    【解决方案3】:

    最重要的是,您必须在真实设备而不是模拟器中运行应用程序。模拟器没有安装google play service 如果您没有任何真实设备,请在安装google play service 后尝试在Genymotion's emulator 中运行您的应用程序。

    您可以查看Running Google Maps v2 on the Android emulator.

    你需要改变

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

    application tag中添加这个

    <meta-data
          android:name="com.google.android.gms.version"
          android:value="@integer/google_play_services_version" />
    

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      相关资源
      最近更新 更多