-1

Trying to execute a MAP-API program in android

MainActivity.java

public class MainActivity extends FragmentActivity {

    // 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 = ((SupportMapFragment) getSupportFragmentManager().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();
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

Manifest

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



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

    <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" >
        <activity
            android:name="info.androidhive.googlemapsv2.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="-----key-------------" />
    </application>

</manifest>

[EDIT]


manifest

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <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" >
        <activity
            android:name="com.example.mapearth.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="---key----" />
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

mainActivity.java

public class MainActivity extends FragmentActivity {

    // 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 = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // check if map is created successfully or not

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

}

enter image description here

I have also

enter image description here

enter image description here



[FINAL EDIT]

log::

[2013-10-19 10:57:21 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/google/android/gms/common/data/Freezable;
[2013-10-19 10:57:21 - MapEarth] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/google/android/gms/common/data/Freezable;
  • Why You Are asking Same Question Again and again ?? – Subhalaxmi Oct 18 '13 at 10:49
  • possible duplicate of [Simple Google maps in android](http://stackoverflow.com/questions/19443620/simple-google-maps-in-android) – EdChum Oct 18 '13 at 10:50
  • @subhalaxmi nayak... I am not able to execute the program .... even though i followed previous steps .... i even followed https://blog-emildesign.rhcloud.com/?p=527 .... to make my emulator compatible still again i get log errors –  Oct 18 '13 at 10:51
  • @ EdCHum .... thats not a duplicate .... this is a new program –  Oct 18 '13 at 10:52
  • use `MapFragment` or change your min sdk to 11 and below – Raghunandan Oct 18 '13 at 10:55
  • check you layout and delete android:name="com.google.android.gms.maps.MapFragment" – Subhalaxmi Oct 18 '13 at 11:00
  • The error is for handler class... so delete the line if (googleMap == null) { Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT) .show(); and run – Subhalaxmi Oct 18 '13 at 11:19
  • The red X means its a broken link path. Just refer to correct path ie /extras/google/google_play_services/libproject/google-play-services_lib library project – Subhalaxmi Oct 18 '13 at 12:22

5 Answers5

4

Map V2:enter image description hereHere check your layout you are using MapFragment & SupportMapFragment so you are getting error Dont Use
android:name="com.google.android.gms.maps.MapFragment"

Only Use

class="com.google.android.gms.maps.SupportMapFragment"

In Manifest: change the package name to yours

<activity
            android:name="com.example.mapearth.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme" >

Select The google play library

Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
  • 1
    Make sure u have added goole play services lib .. Check properties--android than check refference lib – Subhalaxmi Oct 18 '13 at 11:13
  • Add android:name="com.google.android.gms.maps.SupportMapFragment" not android:name="com.google.android.gms.maps.MapFragment" – Subhalaxmi Oct 18 '13 at 11:14
  • @subhalaxminayak `MapFragment` for api 12 and above. and `SupportMapFragment` for 11 and below. If you are targetting just 11 i would suggest to make it 12 and use `MapFramgent` in which case you extend Standard `Activtiy` and use `getFragmentManager` – Raghunandan Oct 18 '13 at 11:20
  • Hmm m saying for 2.2 to 4.3 ie. api 8 to api 18. – Subhalaxmi Oct 18 '13 at 11:24
  • This is only bcoz its not finding the correct path to google play services library or you store this same at two different place. Make sure you are referencing to correct path or else you can just delete the previous one. Download new play service lib , import it and placed it in new place and refer that path.. – Subhalaxmi Oct 18 '13 at 12:10
  • hmm exactly the same prob was with me. so just check if you have the library in two place or two or more different folder in same drive.(by mistake) Just remove the other. – Subhalaxmi Oct 18 '13 at 12:52
  • I did clear the error ..... Have a look here (http://stackoverflow.com/a/19462814/2825729) ..... but i still have 2 error's ...... have to look at the final updated question –  Oct 19 '13 at 05:46
  • Don't b confuse.. although u successfully import it to work space, check that lib may be any where else (u have same lib in two different place) So when you are trying to link with the library , the confusion came as it indicate to two different path. So first time you get green right n next when u check its red cross mark.M I right, If so than check in your drive specially c drive and keep only one lib ... any doubt?? – Subhalaxmi Oct 19 '13 at 06:12
  • Final edit of the Smriti .........shows two errors ... it is caused because .... same multiple jar files in the project( no wrong with code) ( also play services are configured after debugging) .... rectifying it & making a single jar will clear the error ..... ! – Devrath Oct 19 '13 at 08:38
0

Your package name is

  package="com.example.mapearth"

But in activity declaration you have

  <activity
        android:name="info.androidhive.googlemapsv2.MainActivity"

CHange to

  <activity
        android:name="com.example.mapearth.MainActivity"

For api 12 and above use MapFragment else use SupportMapFragment.

If you are using MapFramgent you need to extend standard Activity and use getFragmentManager

Change your min sdk to 11 or below

You can also remove these

<permission
        android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

<uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />

Check out specifying permissions

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

Also remove the below from your xml

  android:name="com.google.android.gms.maps.MapFragment"
  // in case your min sdk is 11 and below use SupportMapFragment 

Edit:

In xml you have

    android:name="com.google.android.gms.maps.MapFragment" 

In activity while initializing you have

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

So change to

    android:name="com.google.android.gms.maps.SupportMapFragment"  

and this

    <uses-sdk
    android:minSdkVersion="11" // 11 or below

Edit:

Refer to the google play services library project properly

Importing google-play-service library showing a red X next to this reference android

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @smriti2 no you have not followed the post properly. check the edit – Raghunandan Oct 18 '13 at 11:12
  • @smriti2 ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment 10-18 11:06:36.595: E/AndroidRuntime(1657): at android.support.v4.app.Fragment.instantiate(Fragment.java:402) is self explanatory – Raghunandan Oct 18 '13 at 11:14
  • 1
    @smriti2 also make sure you have referenced the google play services library project properly and enabled maps fro android in the google api console – Raghunandan Oct 18 '13 at 11:16
  • 1
    @smriti2 check this http://developer.android.com/about/dashboards/index.html. if you are targetting older devices use `SupportMapFragment`. else change your min sdk to 12 and use `MapFragment` everywhere. Check the dashboard before you decide to support for older devices – Raghunandan Oct 18 '13 at 11:18
  • 1
    @smriti2 still you haven't changed the declaration of activity class in manifest. pls follow the post and make the changes. change this ` – Raghunandan Oct 18 '13 at 11:23
  • 1
    @smriti2 you need to refernce google play services library project. check here http://stackoverflow.com/questions/17611017/importing-google-play-service-library-showing-a-red-x-next-to-this-reference-and – Raghunandan Oct 18 '13 at 12:03
  • Hey .... one thing .. i imported the folder to workspace .... but still i get that red cross mark .... as in figure ... i even store it in different location an ... yest it yielded same thing ! ... not able to link ... any ideas –  Oct 18 '13 at 12:26
  • @smriti2 do you follow that post completely or follow here http://developer.android.com/google/play-services/setup.html – Raghunandan Oct 18 '13 at 12:27
  • Yes ... actually i had already done it before ... steps were same as i read on post .... what happening is ... i am able to reference the play services folder correctly .... after i press ... ok ... i can see green arrow mark ... if again i reference back .... by going to properties .... i now see red cross mark ....! ..hope i am clear –  Oct 18 '13 at 12:30
  • Do i need to use perticular selection ... like 4.1.2 or 4.1.3 something like that ? –  Oct 18 '13 at 12:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39491/discussion-between-raghunandan-and-smriti2) – Raghunandan Oct 18 '13 at 12:33
0

try to replace your activity_main.xml file's content by it:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 <fragment
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment" />          
</LinearLayout>
Ba Tới Xì Cơ
  • 482
  • 4
  • 16
0

You need to use the SupportMapFragment in your activity_main.xml like so:

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

The activity is interpreting your XML as a SupportMapFragment however in your XML layout you state it is a normal Map Fragment, causing the error. Change it into a SupportMapFragment and it should interpret it correctly.

Darryl Bayliss
  • 2,677
  • 2
  • 19
  • 28
0

One of the errors i faced was ......... Even though i imported the project to my workspace


  • I was not able to link it ......
  • Spending entire day on that i found out .... what was the error .... (It was a silly one )

enter image description here

I had not selected "copy projects into workspace option"

Now i am able to successfully import it