android - Binary XML file line #2: Error inflating class fragment when create @+id/map -
when app try fragment show follow logcat binary xml file line #1: error inflating class fragment"
here activity_maps.xml layout file:
<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"/>
here permission in androidmanifest.xml:
<application> <permission android:name="xxx.xxx.xxx..permission.maps_receive" android:protectionlevel="signature" /> <uses-permission android:name="xxx.xxx.xxx.permission.maps_receive" /> <uses-permission android:name="android.permission.call_phone" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices"/> <!-- maps api needs opengl es 2.0. --> <uses-feature android:glesversion="0x00020000" android:required="true" /> <!-- end of copy. --> <meta-data android:name="com.google.android.geo.api_key" android:value="apikey" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> </application>
finally java code:
public class mapsactivity extends fragmentactivity implements onmapreadycallback { public googlemap map; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); mapfragment mapfragment = (mapfragment) getfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.maps, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onmapready(googlemap arg0) { // todo auto-generated method stub latlng sydney = new latlng(-33.867, 151.206); map.setmylocationenabled(true); map.movecamera(cameraupdatefactory.newlatlngzoom(sydney, 13)); map.addmarker(new markeroptions() .title("sydney") .snippet("the populous city in australia.") .position(sydney)); } }
all code above load fragment inside swiptab map second tab :
public static class fragmento2 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public static final string arg_section_namber1 = "section_namber"; public fragmento2() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_maps, container, false); return rootview; } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); } }
while try inflate activity_maps on fragment2 show logcat
08-26 21:21:15.597: i/google maps android api(17325): google play services client version: 7571000 08-26 21:21:17.373: i/google maps android api(17325): google play services package version: 7899436
and down:
**"binary xml file line #2: error inflating class fragment when create @+id/map"**
in activity_maps.xml
have used supportmapfragment defining
class="com.google.android.gms.maps.supportmapfragment"
so in activity can not use mapfragment
.you have import supportmapfragment
like
import com.google.android.gms.maps.supportmapfragment;
instead of
import com.google.android.gms.maps.mapfragment;
you can use supportmapfragment
this.
supportmapfragment mapfragment = (supportmapfragment) getfragmentmanager() .findfragmentbyid(r.id.map);
instead of
mapfragment mapfragment = (mapfragment) getfragmentmanager() .findfragmentbyid(r.id.map);
edit:
if using api >= 14, use getfragmentmanager()
. if want compatibility devices below api 14 can use getsupportfragmentmanager()
. therefore, getsupportfragmentmanager()
used deliver newer features older platforms.
if
supportmapfragment mapfragment = (supportmapfragment) getfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this);
does not work try
googlemap googlemap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid( r.id.map)).getmap();
hope helps!
Comments
Post a Comment