我无法使谷歌地图Android API v2工作:GoogleMap无法解析为某种类型

时间:2022-01-11 20:53:34

I -think I- have done all the steps in these two links :

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

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

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

so please don't send them as answers.

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

I have downloaded the Maps API, copied it into Eclipse Workspace, marked it as library, referenced it in my project. But no success. I'm still getting the errors :

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

GoogleMap cannot be resolved to a type

GoogleMap无法解析为某种类型

MapFragment cannot be resolved to a type

MapFragment无法解析为某种类型

Here's my MainActivity.Java:

这是我的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();
    }

}

And here is my AndroidManifest.xml

这是我的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>

Maybe somebody had this error and knows how to solve it ?

也许有人有这个错误,知道如何解决它?

and I have tried restarting eclipse, cleaning the project, creating the project from scratch (its actually v10 or something :) ) apparently I'm doing something wrong..

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

Any kind of help will be appreciated. But please don't paste the two links I have mentioned at the beginning.

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

Edit: Progress Report

@regeme's comment : import com.google.android.gms.maps.GoogleMap; helped getting rid of the error : GoogleMap cannot be resolved to a type

@ regeme的评论:import com.google.android.gms.maps.GoogleMap;帮助摆脱了错误:GoogleMap无法解析为某种类型

However, MapFragment error remains.

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

I have changed the code according @Imtiyaz Khalani's answer. The new code is this :

我根据@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();
            }
        }
    }

But it gives the error : The method getSupportFragmentManager() is undefined for the type MainActivity

但它给出了错误:方法getSupportFragmentManager()未定义类型MainActivity

6 个解决方案

#1


3  

When this error happens, it is because the imports didn't work well. Let's try this, select the Project with Right click and then select Properties. now u go Android -> Library -> add . select google-play-services_lib .

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

obs: u have to import the google_play_services_lib from your sdk directory to your workspace!

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

directory : \sdk\extras\google\google_play_services.

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

please tell me if it worked.

请告诉我它是否有效。

Sorry for this horrible grammar(english isn't my native language).

对不起这个可怕的语法(英语不是我的母语)。

#2


2  

replace

更换

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

to

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

#3


2  

Use FragmentActivity instead of Activity.

使用FragmentActivity而不是Activity。

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

#4


1  

import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.GoogleMap;

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

Manually import the above statement your problem will be solved.

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

#5


0  

If you are getting the following errors, it's means that the GoogleMap class and the MapFragment class can't be found.

如果您收到以下错误,则表示无法找到GoogleMap类和MapFragment类。

As those class are part of the google-play-service library, you have some problem with the way to reference it.

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

You can go over the first three steps of this guide I wrote:

您可以查看我写的本指南的前三个步骤:

Google Maps API V2

Google Maps API V2

and make sure you are making it right.

并确保你做对了。

#6


0  

Your original mapFragment should work just fine after importing the two packages mentioned earlier however you manifest is missing:

在导入前面提到的两个软件包之后,原始的mapFragment应该可以正常工作,但是你的manifest缺失了:

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

#1


3  

When this error happens, it is because the imports didn't work well. Let's try this, select the Project with Right click and then select Properties. now u go Android -> Library -> add . select google-play-services_lib .

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

obs: u have to import the google_play_services_lib from your sdk directory to your workspace!

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

directory : \sdk\extras\google\google_play_services.

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

please tell me if it worked.

请告诉我它是否有效。

Sorry for this horrible grammar(english isn't my native language).

对不起这个可怕的语法(英语不是我的母语)。

#2


2  

replace

更换

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

to

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

#3


2  

Use FragmentActivity instead of Activity.

使用FragmentActivity而不是Activity。

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

#4


1  

import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.GoogleMap;

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

Manually import the above statement your problem will be solved.

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

#5


0  

If you are getting the following errors, it's means that the GoogleMap class and the MapFragment class can't be found.

如果您收到以下错误,则表示无法找到GoogleMap类和MapFragment类。

As those class are part of the google-play-service library, you have some problem with the way to reference it.

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

You can go over the first three steps of this guide I wrote:

您可以查看我写的本指南的前三个步骤:

Google Maps API V2

Google Maps API V2

and make sure you are making it right.

并确保你做对了。

#6


0  

Your original mapFragment should work just fine after importing the two packages mentioned earlier however you manifest is missing:

在导入前面提到的两个软件包之后,原始的mapFragment应该可以正常工作,但是你的manifest缺失了:

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