mobilGelistirici
mobilGelistirici

Reputation: 449

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

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

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 :

GoogleMap cannot be resolved to a type

MapFragment cannot be resolved to a type

Here's my 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

<?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..

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

However, MapFragment error remains.

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

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

Upvotes: 1

Views: 10604

Answers (6)

user2506891
user2506891

Reputation: 143

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

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

Upvotes: 0

Kris
Kris

Reputation: 327

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

Manually import the above statement your problem will be solved.

Upvotes: 1

Thiago Souto
Thiago Souto

Reputation: 771

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 .

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

directory : \sdk\extras\google\google_play_services.

please tell me if it worked.

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

Upvotes: 3

cassioso
cassioso

Reputation: 976

Use FragmentActivity instead of Activity.

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

Upvotes: 2

Emil Adz
Emil Adz

Reputation: 41099

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

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

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

Google Maps API V2

and make sure you are making it right.

Upvotes: 0

Imtiyaz Khalani
Imtiyaz Khalani

Reputation: 2053

replace

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

to

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

Upvotes: 2

Related Questions