【问题标题】:I can't get Google Maps Android API v2 to work : GoogleMap cannot be resolved to a type我无法让 Google Maps Android API v2 工作:GoogleMap 无法解析为一种类型
【发布时间】:2013-11-09 14:12:24
【问题描述】:

我认为我已经完成了这两个链接中的所有步骤:

http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/ http://developer.android.com/google/play-services/setup.html

所以请不要将它们作为答案发送。

我已下载 Maps API,将其复制到 Eclipse Workspace,将其标记为库,并在我的项目中引用它。但没有成功。我仍然收到错误:

GoogleMap 无法解析为类型

MapFragment 无法解析为类型

这是我的 MainActivity.Java:

package com.mapsapp.mapsappv1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends Activity {
 
    
    // Google Map
    private GoogleMap googleMap;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
 
        try {
            // Loading map
            initilizeMap();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
 
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
 
}

这是我的 AndroidManifest.xml

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

<permission
        android:name="com.mapsapp.mapsappv1.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

<uses-permission android:name="com.mapsapp.mapsappv1.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="18" />


    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 
    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <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" >
        
        <!-- this line is part of my trials.. probably unnecessary : -->
        <activity android:name="com.google.android.gms.UnusedStub" />
        
        <activity
            android:name="com.mapsapp.mapsappv1.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>
        
        
        <!-- Google Maps API Key -->
<meta-data
     android:name="com.google.android.maps.v2.API_KEY"
     android:value="myApiKeyGeneratedWithSHA1Key-ItShouldBeTrue" />
     
    </application>

</manifest>

也许有人遇到了这个错误并且知道如何解决它?

我已经尝试重新启动 eclipse,清理项目,从头开始创建项目(实际上是 v10 或其他东西:))显然我做错了什么..

我们将不胜感激任何形式的帮助。但是请不要粘贴我开头提到的两个链接。

编辑:进度报告

@regeme 的评论:import com.google.android.gms.maps.GoogleMap; 帮助摆脱了错误:GoogleMap cannot be resolved to a type

但是,MapFragment 错误仍然存​​在。

我已根据@Imtiyaz Khalani 的回答更改了代码。新代码是这样的:

private void initilizeMap() {
        if (mMap == null) {
            //googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // check if map is created successfully or not
            if (mMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

但它给出了错误:The method getSupportFragmentManager() is undefined for the type MainActivity

【问题讨论】:

  • 您导入了 import com.google.android.gms.maps.GoogleMap 吗?
  • @regeme 导入它解决了 GoogleMap 问题。谢谢!另一方面,我希望 Imtiyaz Khalani 的回答能解决它:)

标签: java android eclipse google-maps google-maps-android-api-2


【解决方案1】:

发生此错误时,是因为导入效果不佳。 让我们试试这个,右键单击选择项目,然后选择属性。 现在你去 Android -> 图书馆 -> 添加 . 选择 google-play-services_lib 。

obs:您必须将 google_play_services_lib 从您的 sdk 目录导入到您的工作区!

目录:\sdk\extras\google\google_play_services。

请告诉我它是否有效。

对这种可怕的语法感到抱歉(英语不是我的母语)。

【讨论】:

  • 我能够导入并添加到我的项目中,但它并不能解决我的问题。但我相信这是朝着正确方向迈出的重要一步。
【解决方案2】:

替换

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

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

【讨论】:

  • 我没听错吧?您是在说“删除所有 googleMap 引用,并声明 SupportMapFragment mMap;”,对吗?
  • 如果我将 mMap 定义为 SupportMapFragment:错误:Type mismatch: cannot convert from GoogleMap to SupportMapFragment 如果 mMap 是 GoogleMap,那么:Type mismatch: cannot convert from GoogleMap to SupportMapFragment
【解决方案3】:

【讨论】:

    【解决方案4】:

    导入 com.google.android.gms.maps.MapFragment; 导入 com.google.android.gms.maps.GoogleMap;

    手动导入上面的语句你的问题就解决了。

    【讨论】:

      【解决方案5】:

      如果出现以下错误,则表示找不到GoogleMap 类和MapFragment 类。

      由于这些类是 google-play-service 库的一部分,因此您在引用它的方式上存在一些问题。

      您可以复习我编写的本指南的前三个步骤:

      Google Maps API V2

      并确保你做对了。

      【讨论】:

        【解决方案6】:

        在导入前面提到的两个包后,您的原始 mapFragment 应该可以正常工作,但是您的清单丢失了:

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

        【讨论】:

          猜你喜欢
          • 2016-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多