java - NullPointerException at mapFragment.getMapAsync -
after going through questions relating title, couldn't find solution problem. having nullpointerexception
@ mapfragment.getmapasync
. below mapactivity
code.
package com.example.jerofad.mylocation; import android.app.fragmentmanager; import android.support.v4.app.fragmentactivity; import android.os.bundle; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.onmapreadycallback; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; public class mapsactivity extends fragmentactivity implements onmapreadycallback { private googlemap mmap; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) this.getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); } @override public void onmapready(googlemap googlemap) { mmap = googlemap; // add marker in sydney , move camera latlng sydney = new latlng(-34, 151); mmap.addmarker(new markeroptions().position(sydney).title("marker in sydney")); mmap.movecamera(cameraupdatefactory.newlatlng(sydney)); mmap.getuisettings().setzoomcontrolsenabled(true); } }
i using google play service 8.4.0
. android manifest given below.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.jerofad.mylocation"> <!-- access_coarse/fine_location permissions not required use google maps android api v2, must specify either coarse or fine location permissions 'mylocation' functionality. --> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" /> <uses-permission android:name="android.permission.write_external_storage" android:maxsdkversion="22"/> <permission android:name="com.example.jerofad.mylocation.permission.maps_receive" android:protectionlevel="signature" /> <uses-permission android:name="com.example.jerofad.mylocation.permission.maps_receive" /> <uses-permission android:name="android.permission.read_external_storage" android:maxsdkversion="22" /> <uses-feature android:glesversion="0x00020000" android:required="true" /> <application android:allowbackup="true" android:icon="@drawable/mylocation" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <meta-data android:name="com.google.android.geo.api_key" android:value="@string/google_maps_key" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name=".mapsactivity" android:label="@string/title_activity_maps"> </activity> <activity android:name=".launcheractivity" android:label="@string/title_activity_launcher" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
how can fix error @ mapfragment.getmapasync(this)
. in advance. here crash log.
04-25 11:11:49.079 31448-31448/com.example.jerofad.mylocation e/androidruntime: fatal exception: main process: com.example.jerofad.mylocation, pid: 31448 java.lang.runtimeexception: unable start activity componentinfo{com.example.jerofad.mylocation/com.example.jerofad.mylocation.mapsactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2338) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2390) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1321) @ android.os.handler.dispatchmessage(handler.java:110) @ android.os.looper.loop(looper.java:193) @ android.app.activitythread.main(activitythread.java:5299) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:825) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:641) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.example.jerofad.mylocation.mapsactivity.oncreate(mapsactivity.java:24) @ android.app.activity.performcreate(activity.java:5423) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1088) @ android.app.activitythread.performlaunchactivity(activitythread.java:2302) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2390) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1321) @ android.os.handler.dispatchmessage(handler.java:110) @ android.os.looper.loop(looper.java:193) @ android.app.activitythread.main(activitythread.java:5299) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:825) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:641) @ dalvik.system.nativestart.main(native method)
here activity_maps.xml.
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" class ="com.google.android.gms.maps.mapfragment" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.jerofad.mylocation.mapsactivity" />
please try this,
remove/comment supportmapfragment line while , try this,
mapfragment mapfragment = (mapfragment) getfragmentmanager() .findfragmentbyid(r.id.map);
Comments
Post a Comment