【问题标题】:Issues with Google Maps on Android TutorialAndroid 教程上的 Google 地图问题
【发布时间】:2014-01-20 03:41:01
【问题描述】:

大家好,我一直在努力学习 Google 在其开发人员网站上提供的关于在 Android 中使用 Google 地图的教程。不用说,我无法让这件事正常工作,而且它有多个错误。所以我从 Youtube 切换到一个,这也给了我错误。我想做的就是让地图出现。我无法使用的错误发布在下面。我真的希望你们能给我一个线索,告诉我该怎么做。非常感谢任何帮助。

人们还注意到,每次我遇到问题时,我都会用谷歌搜索并尝试多个“修复”。所以代码中的任何不一致都是由此带来的。

错误:

Gradle:任务 ':GoogleMapsDemoV2:compileDebug' 执行失败。

编译失败;有关详细信息,请参阅编译器错误输出。

C:\Users\Josebz\AndroidStudioProjects\GoogleMapsDemoV2Project\GoogleMapsDemoV2\src\main\java\com\example\googlemapsdemov2\MainActivity.java

这里是我所有的代码...

主要活动

package com.example.googlemapsdemov2;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

Android 清单

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

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

    <permission
        android:name="com.example.googlemapsdemov2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.googlemapsdemov2.permission.MAPS_RECEIVE"/>

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

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

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

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

</manifest>

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


    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:support-v13:13.0.+'
    compile 'com.google.android.gms:play-services:3.2.25'
}

【问题讨论】:

  • 你的问题解决了吗?
  • 不幸的是还没有
  • 另外,伙计们,我的 MainActivity.java 上出现红色波浪线,它告诉我“无法解析符号 R”。我在网上寻找解决方案,并确保任何地方都没有“import android.R”语句。据我了解,这可能是由于布局文件中的错误引起的,而我看到的唯一接近错误的事情是在 activity_main 中,这是关于布局渲染的警告。任何人都可以执行此代码并让我知道它是否适合你们所有人? @AndroidHacker

标签: android google-maps gradle build-error


【解决方案1】:

在 AndroidManifest.xml 中的 MAP_KEY 行之前添加以下两行

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

【讨论】:

  • 您确定他/她发布的错误与您的回答有关吗?请解释一下这对我们来说也是很好的学习
  • 不。这就是我得到的:Gradle:找不到与给定名称匹配的资源(在“值”处,值为“@integer/google_play_services_version”)这很奇怪,因为我很确定我的 GooglePlayServices 是最新的
【解决方案2】:

1- 首先,您必须确保您的 SDK 中是否安装了 GOOGLE PLAY SERVICES

为此打开EclipseWinhowsAndroi SDK Manager 并检查是否已经下载?

2- 将其导入 Eclipse

为此在 Eclipse 中转到 FileImportAndroidExisting Android Code Into Workspace

并从

找到播放服务库项目

android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib

3- 谷歌地图获取谷歌地图密钥的非常重要和要点

为您转到 Eclipse 窗口选项卡 > 首选项 > Android > 构建,然后复制您的 SHA-1 指纹。

然后跟随这个link 然后转到Services 选项卡然后搜索Google Map API v2 并打开它ONGoofle Play Android Dev API 相同然后导航到API ACCESS 选项卡然后Create new Android key..

谢谢

【讨论】:

  • 鉴于他遇到 Gradle 错误,他使用的是 Android Studio 而不是 Eclipse。因此这些指示不适用。
  • 汉尼拔说了什么。无论哪种方式,我都做了这一切。正如您在我的代码中看到的,我已经包含了 Google 给我的密钥,为此我需要给他们我的计算机生成的 SHA-1。
【解决方案3】:

首先尝试使用清单文件的应用程序部分下提到的..

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

例如:您的清单文件..

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

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

    <permission
        android:name="com.example.googlemapsdemov2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.googlemapsdemov2.permission.MAPS_RECEIVE"/>

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

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

        <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
       <!--Your Google API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAL5Xq8scMIHnWXITSOLN9QipVkri4A-lI"/>

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

</manifest>

其次,如果您在较低版本的 android 上测试相同,则需要使用 SupportMapFragment。像这样尝试..

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

希望它能解决您的问题,如果有任何问题,请告诉我..

【讨论】:

  • 尝试了你给我的第一个解决方案,没有任何变化。您告诉我的第二件事(“地图片段”)已经在我的代码中,因此无需更改任何内容。这些是我目前遇到的错误......'Gradle:任务':GoogleMapsDemoV2:processDebugResources'的执行失败。 > 无法在任务“:GoogleMapsDemoV2:processDebugResources”和“Gradle:找不到与给定名称匹配的资源(在“值”,值为“@integer/google_play_services_version”)上调用 IncrementalTask​​.taskAction()。
  • 我也尝试用版本号“4132530”替换“@integer/google_play_services_version”部分。然后弹出其他错误:link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 2011-01-03
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多