Android GoogleMap or SupportMapFragment - null pointer exception -
in app showing google map version2 in fragment. null pointer exception @
mmap = ((supportmapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap();
this full code:
public class searchresultmap extends fragment{ private googlemap mmap; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view fragmentview = inflater.inflate(r.layout.map, container, false); mmap = ((supportmapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); // line no : 28 mmap.addmarker(new markeroptions() .position(new latlng(xxxxxx,xxxxxx)) .title("current location") .icon(bitmapdescriptorfactory.fromresource(r.drawable.ball_pointer)) .snippet("xxxxx")); return fragmentview; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } }
xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.supportmapfragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
manifest xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fssd.spot" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <permission android:name="com.fssd.spot.permission.maps_receive" android:protectionlevel="signature" /> <uses-feature android:glesversion="0x00020000" android:required="true" /> <uses-permission android:name="com.fssd.spot.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-permission android:name="android.permission.access_network_state" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.fssd.spot.splashscreen" android:theme="@android:style/theme.light.notitlebar" android:nohistory="true" android:screenorientation="portrait" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.fssd.spot.mainactivity" android:screenorientation="portrait" android:theme="@style/tabtheme" > </activity> <meta-data android:name="com.google.android.maps.v2.api_key" android:value="xxxxxxxxxxxxxxxxxxxxxxxx" /> </application> </manifest>
whole log cat:
fatal exception: main java.lang.nullpointerexception @ com.fssd.spot.search.searchresultmap.oncreateview(searchresultmap.java:28) @ android.support.v4.app.fragment.performcreateview(fragment.java:1478) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:927) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1460) @ android.support.v4.app.fragment.performstart(fragment.java:1499) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:957) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1460) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:440) @ android.os.handler.handlecallback(handler.java:587) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:130) @ android.app.activitythread.main(activitythread.java:3683) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:507) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:875) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:633) @ dalvik.system.nativestart.main(native method)
try change this:
fragmentmanager myfm = getactivity().getsupportfragmentmanager(); final supportmapfragment mymapf = (supportmapfragment) myfm .findfragmentbyid(r.id.map);
Comments
Post a Comment