Error android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment -
i following tutorial using google maps api v2 .i facing error error logs
05-15 00:08:18.415: e/androidruntime(4547): fatal exception: main 05-15 00:08:18.415: e/androidruntime(4547): java.lang.runtimeexception: unable start activity componentinfo{org.rtesh.onemore/org.rtesh.onemore.mainactivity}: android.view.inflateexception: binary xml file line #7: error inflating class fragment 05-15 00:08:18.415: e/androidruntime(4547): @ android.app.activitythread.performlaunchactivity(activitythread.java:1830) 05-15 00:08:18.415: e/androidruntime(4547): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1851) 05-15 00:08:18.415: e/androidruntime(4547): @ android.app.activitythread.access$1500(activitythread.java:132) 05-15 00:08:18.415: e/androidruntime(4547): @ android.app.activitythread$h.handlemessage(activitythread.java:1038) 05-15 00:08:18.415: e/androidruntime(4547): @ android.os.handler.dispatchmessage(handler.java:99) 05-15 00:08:18.415: e/androidruntime(4547): @ android.os.looper.loop(looper.java:150) 05-15 00:08:18.415: e/androidruntime(4547): @ android.app.activitythread.main(activitythread.java:4277) 05-15 00:08:18.415: e/androidruntime(4547): @ java.lang.reflect.method.invokenative(native method) 05-15 00:08:18.415: e/androidruntime(4547): @ java.lang.reflect.method.invoke(method.java:507) 05-15 00:08:18.415: e/androidruntime(4547): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 05-15 00:08:18.415: e/androidruntime(4547): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 05-15 00:08:18.415: e/androidruntime(4547): @ dalvik.system.nativestart.main(native method) 05-15 00:08:18.415: e/androidruntime(4547): caused by: android.view.inflateexception: binary xml file line #7: error inflating class fragment 05-15 00:08:18.415: e/androidruntime(4547): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:587) 05-15 00:08:18.415: e/androidruntime(4547): @ android.view.layoutinflater.rinflate(layoutinflater.java:623) 05-15 00:08:18.415: e/androidruntime(4547): @ android.view.layoutinflater.inflate(layoutinflater.java:408) 05-15 00:08:18.415: e/androidruntime(4547): @ android.view.layoutinflater.inflate(layoutinflater.java:320) 05-15 00:08:18.415: e/androidruntime(4547): @ android.view.layoutinflater.inflate(layoutinflater.java:276) 05-15 00:08:18.415: e/androidruntime(4547): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:250) 05-15 00:08:18.415: e/androidruntime(4547): @ android.app.activity.setcontentview(activity.java:1712) 05-15 00:08:18.415: e/androidruntime(4547): @ org.rtesh.onemore.mainactivity.oncreate(mainactivity.java:27) 05-15 00:08:18.415: e/androidruntime(4547): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1072) 05-15 00:08:18.415: e/androidruntime(4547): @ android.app.activitythread.performlaunchactivity(activitythread.java:1794) 05-15 00:08:18.415: e/androidruntime(4547): ... 11 more 05-15 00:08:18.415: e/androidruntime(4547): caused by: android.support.v4.app.fragment$instantiationexception: unable instantiate fragment com.google.android.gms.maps.mapfragment: make sure class name exists, public, , has empty constructor public 05-15 00:08:18.415: e/androidruntime(4547): @ android.support.v4.app.fragment.instantiate(fragment.java:395) 05-15 00:08:18.415: e/androidruntime(4547): @ android.support.v4.app.fragment.instantiate(fragment.java:363) 05-15 00:08:18.415: e/androidruntime(4547): @ android.support.v4.app.fragmentactivity.oncreateview(fragmentactivity.java:264) 05-15 00:08:18.415: e/androidruntime(4547): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:563) 05-15 00:08:18.415: e/androidruntime(4547): ... 20 more 05-15 00:08:18.415: e/androidruntime(4547): caused by: java.lang.classnotfoundexception: com.google.android.gms.maps.mapfragment in loader dalvik.system.pathclassloader[/data/app/org.rtesh.onemore-1.apk] 05-15 00:08:18.415: e/androidruntime(4547): @ dalvik.system.pathclassloader.findclass(pathclassloader.java:240) 05-15 00:08:18.415: e/androidruntime(4547): @ java.lang.classloader.loadclass(classloader.java:551) 05-15 00:08:18.415: e/androidruntime(4547): @ java.lang.classloader.loadclass(classloader.java:511) 05-15 00:08:18.415: e/androidruntime(4547): @ android.support.v4.app.fragment.instantiate(fragment.java:385) 05-15 00:08:18.415: e/androidruntime(4547): ... 23 more
my mainactivity class
@suppresslint("newapi") public class mainactivity extends fragmentactivity { static final latlng hamburg = new latlng(53.558, 9.927); static final latlng kiel = new latlng(53.551, 9.993); private googlemap map; @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); if (map!=null){ marker hamburg = map.addmarker(new markeroptions().position(hamburg) .title("hamburg")); marker kiel = map.addmarker(new markeroptions() .position(kiel) .title("kiel") .snippet("kiel cool") .icon(bitmapdescriptorfactory .fromresource(r.drawable.ic_launcher))); } }
}
and activity_main.xml is
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.mapfragment" /> </relativelayout>
i gave appropriate permissions in maifest class
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.rtesh.onemore" android:versioncode="1" android:versionname="1.0" > <permission android:name="org.rtesh.android.locationapi.maps.permission.maps_receive" android:protectionlevel="signature" /> <uses-feature android:glesversion="0x00020000" android:required="true" /> <uses-permission android:name="com.org.rtesh.android.locationapi.maps.permission.maps_receive" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="org.rtesh.onemore.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> <meta-data android:name="com.google.android.maps.v2.api_key" android:value="mykey" /> </application> </manifest>
i referenced google play service library why facing error ??please 1 , please point major differences between google maps version1 , 2!!
just try replace layout :
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:name="com.google.android.gms.maps.supportmapfragment" android:layout_width="match_parent" android:layout_height="match_parent" />
you need use supportmapfragment
api under 11 !
in mainactivity (or in each activity want put v2 map) must extend fragmentactivity or activity either want use supportmapfragment or mapfragment. use mapfragment class if targeting api 12 , above. otherwise, use supportmapfragment. it's subtle difference makes error.
edit:
please change code in fragmentactivity:
map = ((supportmapfragment) getfragmentmanager().findfragmentbyid(r.id.map)) .getmap();
understand concept. since targeting lower android versions fragments not supported, need use support library takes care of fragments. wherever using fragments make sure importing fragment support
library.
Comments
Post a Comment