繁体   English   中英

我无法使Google Maps Android API v2正常工作:GoogleMap无法解析为某种类型

[英]I can't get Google Maps Android API v2 to work : GoogleMap cannot be resolved to a type

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

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

发生此错误时,是因为导入效果不佳。 让我们尝试一下,右键单击选择Project,然后选择Properties。 现在你去Android-> Library-> add。 选择google-play-services_lib。

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

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

请告诉我它是否有效。

对不起,这个语法太糟糕了(英语不是我的母语)。

更换

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

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

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

手动导入以上语句将解决您的问题。

如果出现以下错误,则意味着MapFragment GoogleMap类和MapFragment类。

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

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

Google Maps API第2版

并确保您做对了。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM