android - ClassCastException when trying to get Google Play Services Map v2 to display -


i've been beating me head against wall few days now. i've read every post can on topic , nothing seems me across finish line.

trying build app using google play services map v2 api.

i've got api key, no problem.

here 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 menu; adds items action bar if 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 noclassdeffound error involving r$styleable, , classnotfound of maps class, cannot past one.

regarding class, i've tried couple different implementations without luck. 1 seems explicit. no compile warnings.

my target android-17 min of android-11. i've got library imported (with copy files option), , referenced project, , exported too.

obviously there's i'm missing. please help!

thanks, david

you can't cast mapfragment (from xml) supportmapfragment.

one possible solution change mapfragment in xml supportmapfragment ankitmakwana suggests in answer.

a better approach in opinion set minimal supported api api12 instead of api11 , use mapfragment instead of supportmapfragment. don't have worry compatability library.

setting minimal supported api api12 instead of api11 shouldn't render (almost) devices uncompatible app, according this answer posted 1 of biggest android gurus here in so, there no devices running api11.

if chose go way, keep xml , change following lines in code 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 work then.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -