java - Google Maps: Create marker from different class -
in android studio project, have 2 activites: 1 mapsactivity , other basic.
when click on map, other activity launches consists of set of edittexts (textfields) allowing user define custom properties marker. however, when try create marker class, android studio gives me error.
here code, mapsactivity.java:
package com.geekybrackets.virtualtourguide; import android.content.intent; 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.marker; import com.google.android.gms.maps.model.markeroptions; public class mapsactivity extends fragmentactivity implements onmapreadycallback { public googlemap mmap; double lat = 0; double lon = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); } /** * manipulates map once available. * callback triggered when map ready used. * can add markers or lines, add listeners or move camera. in case, * add marker near sydney, australia. * if google play services not installed on device, user prompted install * inside supportmapfragment. method triggered once user has * installed google play services , returned app. */ @override public void onmapready(googlemap googlemap) { mmap = googlemap; mmap.setonmapclicklistener(new googlemap.onmapclicklistener() { public void onmapclick(latlng latlng) { lat = latlng.latitude; lon = latlng.longitude; startactivity(new intent(mapsactivity.this, newmarker.class)); } }); } }
newmarker.java (activity user enters properties of marker)
package com.geekybrackets.virtualtourguide; import android.content.intent; import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.widget.button; import android.widget.edittext; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; public class newmarker extends appcompatactivity{ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_new_marker); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); button save_btn = (button)findviewbyid(r.id.btn_save); save_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { edittext editname = (edittext)findviewbyid(r.id.editname); string marker_title = editname.gettext().tostring(); mapsactivity = new mapsactivity(); i.mmap.addmarker(new markeroptions() .position(new latlng(i.lat, i.lon)) .title(marker_title)); i.mmap.movecamera(cameraupdatefactory.newlatlng(new latlng(i.lat, i.lon))); finish(); } }); } }
this error get:
java.lang.nullpointerexception: attempt invoke virtual method 'com.google.android.gms.maps.model.marker com.google.android.gms.maps.googlemap.addmarker(com.google.android.gms.maps.model.markeroptions)' on null object reference @ com.geekybrackets.virtualtourguide.newmarker$2.onclick(newmarker.java:42)
how solve issue? input wil appreciated :)
oh boy, you've got couple of things learn.
first, should never invoke activity
's constructor - idea framework creates instance of activity
, handle initialization logic on oncreate(bundle savedinstancestate)
. if call constructor, create random object not going part of ui, let alone instance if mapsactivity
want work.
second, newmarker
horrible class name, should keep java convention , start class names upper case letter, newmarkeractivity
,
third, if want update mapsactivity
activity
, need start second activity for result first activity. basically, should call startactivityforresult(new intent(mapsactivity.this, newmarkeractivity.class))
instead of current call, , override onactivityresult
in mapsactivity
. pass title in result bundle
, retrieve in onactivityresult
.
consult documentation getting results
edit: pass title, need create intent
, pass in title call intent.putextra("somekey", thetitle)
. afterwards, call activity.setresult(int, intent)
intent
. in onactivityresult
, can title calling getstringextra("somekey")
it's idea make key public static final
field of newmarkeractivity
class, don't make typo.
Comments
Post a Comment