0

I've been beating me head against a wall with this for a few days now. I've read just about every post I can on the topic and nothing seems to get me across the finish line.

Trying to build an app using the Google Play Services Map v2 API.

I've got the API key, no problem.

Here are my bits: MainActivity.java

public class MainActivity extends FragmentActivity
{

    GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
        if ( status==ConnectionResult.SUCCESS)
        {
            SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            googleMap = supportMapFragment.getMap();
        }
        else
        {
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/map"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.MapFragment"/>

Relevant manifest:

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

<permission
    android:name="com.mcdaniel.mmm4.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.mcdaniel.mmm4.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.persmission.INTERNET" />
<uses-permission android:name="android.persmission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.persmission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="MY API KEY" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

I've got past the NoClassDefFound error involving R$styleable, and ClassNotFound of the maps class, but I cannot get past this one.

Regarding the class, I've tried a couple different implementations without luck. This one seems the most explicit. No compile warnings.

My target is android-17 with min of android-11. I've got the library imported (with copy files option), and referenced by my project, and exported too.

Obviously there's something I'm missing. Please help!

Thanks, David

Mac
  • 48
  • 1
  • 9

3 Answers3

1

this may helps you in xml change Like SupportMapFragment

<fragment
            android:id="@+id/chk_aerrow_map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="fill_parent"
            android:layout_margin="5dp"
            android:layout_height="100dp" />

for Details see Here

Community
  • 1
  • 1
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
  • 1
    Simple answer. Zabri and Raghunandan could "fight" less and make their (also mostly correct) answers concise. – MaciejGórski May 03 '13 at 09:57
  • Well that helped, but now I'm back to NoClassDefFoundError: com.google.android.gms.R$styleable... so strange. Thank you ankitmakwana! – Mac May 03 '13 at 20:24
  • I actually switched back to MapFragment as I really wasn't fond of having to use the support library. Also switch min=12 as noted below. – Mac May 03 '13 at 20:40
0
         My target is android-17 with min of android-11

Your min sdk is 11.

Note : If you wish to run the app on api 11 and lower You will need to add support library as well.

http://developer.android.com/tools/extras/support-library.html

Use this

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

Since you din't add support library you got classcastexception

Also make sure you have followed all the steps from the below link

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

Edit

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

MapFragment for api 12 and higher support fragment for api 11 and lowever

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • API11 **is** Honeycomb. No need for a support library. – zbr May 03 '13 at 07:59
  • @Zabri i just answered a similar question here http://stackoverflow.com/questions/16322472/google-maps-api-android-error-inflating-class-fragment/16323629#16323629. Worked for the user. Pls check before downvoting – Raghunandan May 03 '13 at 08:03
  • @Zabri check the link in the edit for more information. check the last statement in the snap shot attached. you should support your claim before adding a comment. – Raghunandan May 03 '13 at 08:10
  • 1
    Yes, you're right. Removing the downvote now. But please still check the edit I made to my answer. – zbr May 03 '13 at 08:14
  • @Zabri if he uses support fragment he must add support library. – Raghunandan May 03 '13 at 08:15
  • Yeah, what I am saying that if he supported min. API12 (which according to CommonsWare's answer from the question that I posted a link to in my answer is the same as supporting min. API11), then there is no need for him to use `SupportMapFragment`. He could, though. But no need. – zbr May 03 '13 at 08:18
  • Can you support the documentation saying API 12 is required? There were a lot of errors in these docs and this seem like one that is not yet corrected. – MaciejGórski May 03 '13 at 10:00
  • dd you check the snap shot of the edit. check the last line. I don't know if docs contain error. I cannot comment on that – Raghunandan May 03 '13 at 10:02
  • OK, switched to min=12. I just want to not eliminate a ton of devices. – Mac May 03 '13 at 20:32
  • I also removed reference to support FragmentActivity, so now my class extends Activity. I checked over the two links and followed the 1st 100% to get my initial code, and as I posted above, use the getMap() to actually get the map. I also now have a LinearLayout surrounding my map fragment in my .xml file. – Mac May 03 '13 at 20:38
0

You can't cast a MapFragment (from your XML) to a SupportMapFragment.

One possible solution is to change the MapFragment in your XML to a SupportMapFragment as ankitmakwana suggests in his answer.

A better approach would in my opinion be to set minimal supported API to API12 instead of API11 and use a MapFragment instead of a SupportMapFragment. Then you don't have to worry about a compatability library.

Setting minimal supported API to API12 instead of API11 shouldn't render (almost) any devices uncompatible with your app, as according to this answer posted by one of the biggest Android gurus here in SO, there are basically no devices running API11.

If you chose to go this way, just keep your XML as it is and change the following lines in your code from this:

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

to this:

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

It will work then.

Community
  • 1
  • 1
zbr
  • 6,860
  • 4
  • 31
  • 43
  • Thanks for your advice!! I've got a lot further finally! I changed to min=12, got rid of all references to the support.v4 packages and now I'm not getting those errors above. But what I am getting is "Failed to load map. Could not contact Google servers. Please add into AndroidManifest.xml. But it's already there! Not sure what's going on now. – Mac May 03 '13 at 21:01
  • I think there should be `` instead of ``. Please try to edit it and let me know if it worked. – zbr May 03 '13 at 21:16
  • I've got those too, but the error specifically references ACCESS_NETWORK_STATE, which I've got too. – Mac May 03 '13 at 21:22
  • So does it work now? If so, make sure to accept an answer here. (Does not necessarily have to be mine. ;)) Thank you. – zbr May 03 '13 at 21:28
  • 1
    Yes, it finally works! Thanks to all that helped me get there! I will accept one of the answers, although there was no single one that got me there.. it was a combination of all of your help! Thanks again! – Mac May 03 '13 at 22:31