i have google map in fragment in application. funny thing map loads, logcat displays nullpointerexception, , noticed when tried update camera on map automatically zoom in on marker. map crash. otherwise, map loads fine in fragment no marker (even though adding 1 using current gps location).
edited*
i understand crashing because googlemap null, don't understand why it's returning null. map renders , i've done before , crashes @ point. doesn't render.
here logcat error
05-08 01:30:33.143 30130-30130/com.example.javed_000.famjam e/mapapp﹕ java.lang.nullpointerexception: attempt invoke virtual method 'com.google.android.gms.maps.googlemap com.google.android.gms.maps.supportmapfragment.getmap()' on null object reference 05-08 01:30:33.152 30130-30130/com.example.javed_000.famjam e/mapfragment﹕ lat = 10.7270913 , long = -61.5544451 05-08 01:30:33.155 30130-30130/com.example.javed_000.famjam d/androidruntime﹕ shutting down vm 05-08 01:30:33.156 30130-30130/com.example.javed_000.famjam e/androidruntime﹕ fatal exception: main process: com.example.javed_000.famjam, pid: 30130 java.lang.nullpointerexception: attempt invoke virtual method 'void com.google.android.gms.maps.googlemap.animatecamera(com.google.android.gms.maps.cameraupdate)' on null object reference @ com.example.javed_000.famjam.mapfragment.getlocation(mapfragment.java:60) @ com.example.javed_000.famjam.mapfragment.oncreateview(mapfragment.java:33) @ android.support.v4.app.fragment.performcreateview(fragment.java:1786) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:953) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1136) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:739) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1499) @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:488) @ android.support.v4.app.fragmentstatepageradapter.finishupdate(fragmentstatepageradapter.java:163) @ android.support.v4.view.viewpager.populate(viewpager.java:1073) @ android.support.v4.view.viewpager.setcurrentiteminternal(viewpager.java:555) @ android.support.v4.view.viewpager.setcurrentiteminternal(viewpager.java:514) @ android.support.v4.view.viewpager.setcurrentitem(viewpager.java:495) @ com.example.javed_000.famjam.slidingtablayout$tabclicklistener.onclick(slidingtablayout.java:283) @ android.view.view.performclick(view.java:4780) @ android.view.view$performclick.run(view.java:19866) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
this manifest file
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.get_accounts" /> <uses-permission android:name="android.permission.use_credentials" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.access_network_state"/> <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" /> <application android:allowbackup="true" android:icon="@drawable/launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.maps.v2.api_key" android:value="aizasyahgnz7esl-eirs4da_hgw_7u9iqi3sze0" /> <activity android:name=".signinactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".mainactivity" android:label="@string/app_name" > </activity> </application>
this dependencies in gradle file
dependencies { compile filetree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' compile 'com.google.android.gms:play-services:7.0.0' compile 'com.google.android.gms:play-services-maps:7.0.0'
}
this layout file.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/mapview" android:name="com.google.android.gms.maps.supportmapfragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </relativelayout>
here code snippet map fragment
private void createmapview(){ try { if(googlemap == null){ supportmapfragment smf = (supportmapfragment) getactivity().getsupportfragmentmanager().findfragmentbyid(r.id.mapview); googlemap = smf.getmap(); if(googlemap == null) { toast.maketext(getactivity().getbasecontext(), "error creating map", toast.length_short).show(); } } } catch (nullpointerexception exception){ log.e("mapapp", exception.tostring()); } } private void getlocation(){ gps = new gpstracker(this.getactivity()); if(gps.cangetlocation()){ latitude = gps.getlatitude(); longitude = gps.getlongitude(); log.e("mapfragment","lat = " + latitude + " , long = " + longitude); addmarker(latitude,longitude); googlemap.animatecamera(cameraupdatefactory.newlatlngzoom(new latlng(latitude,longitude), 12)); } else { gps.showsettingsalert(); } } private void addmarker(double lat, double lng){ if(googlemap != null){ googlemap.addmarker(new markeroptions() .position(new latlng(lat, lng)) .title("marker") .draggable(true) ); } }
googlemap = ((supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map)).getmap();
this worked me. getsupportfragmentmanager()
seemed causing crashes.
Comments
Post a Comment