android - Getting java.lang.NullPointerException when I try to use maps -


first of all, i'm using android studio , it's first time using maps.

i created project, navigation drawer activity.

file> new project > next > next> switched "navigation drawer activity" , clicked next > finish.

after, clicked in project right button > new > activity > google > google maps activity.

i put google api maps key in google_maps_api.xml.

so opened mainactivity, , type following inside oncreateview function:

@override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     view rootview = inflater.inflate(r.layout.activity_maps, container, false);     setupmapifneeded();     return rootview; } 

then add after following 2 functions:

 private void setupmapifneeded() {         // null check confirm have not instantiated map.         if (mmap == null) {             // try obtain map supportmapfragment.             mmap = ((supportmapfragment) getactivity().getsupportfragmentmanager().findfragmentbyid(r.id.map))                     .getmap();             // check if successful in obtaining map.             if (mmap != null) {                 setupmap();             }         }    }     private void setupmap() {         mmap.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker"));     } 

everytime try execute it, null pointer exception. here complete error message:

04-14 19:44:40.204  4390-4390/ct.ufrn.br.myapplication e/androidruntime﹕ fatal exception: main     process: ct.ufrn.br.myapplication, pid: 4390     java.lang.runtimeexception: unable start activity componentinfo{ct.ufrn.br.myapplication/ct.ufrn.br.myapplication.mainactivity}: java.lang.nullpointerexception             @ android.app.activitythread.performlaunchactivity(activitythread.java:2198)             @ android.app.activitythread.handlelaunchactivity(activitythread.java:2257)             @ android.app.activitythread.access$800(activitythread.java:139)             @ android.app.activitythread$h.handlemessage(activitythread.java:1210)             @ android.os.handler.dispatchmessage(handler.java:102)             @ android.os.looper.loop(looper.java:136)             @ android.app.activitythread.main(activitythread.java:5086)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:515)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601)             @ dalvik.system.nativestart.main(native method)     caused by: java.lang.nullpointerexception             @ ct.ufrn.br.myapplication.mainactivity$placeholderfragment.oncreateview(mainactivity.java:142)             @ android.support.v4.app.fragment.performcreateview(fragment.java:1786)             @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:947)             @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1126)             @ android.support.v4.app.backstackrecord.run(backstackrecord.java:739)             @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1489)             @ android.support.v4.app.fragmentactivity.onstart(fragmentactivity.java:548)             @ android.app.instrumentation.callactivityonstart(instrumentation.java:1194)             @ android.app.activity.performstart(activity.java:5258)             @ android.app.activitythread.performlaunchactivity(activitythread.java:2171)             @ android.app.activitythread.handlelaunchactivity(activitythread.java:2257)             @ android.app.activitythread.access$800(activitythread.java:139)             @ android.app.activitythread$h.handlemessage(activitythread.java:1210)             @ android.os.handler.dispatchmessage(handler.java:102)             @ android.os.looper.loop(looper.java:136)             @ android.app.activitythread.main(activitythread.java:5086)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:515)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601)             @ dalvik.system.nativestart.main(native method) 

do know how solve it?

obs: tryied same blank activity (in oncreate method, instead oncreateview), , worked well.

here activity_maps.xml:

<fragment 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" android:id="@+id/map" tools:context="ct.ufrn.br.myapplication.mapsactivity" android:name="com.google.android.gms.maps.supportmapfragment" /> 

i solved replacing

mmap = ((supportmapfragment) getactivity().getsupportfragmentmanager().findfragmentbyid(r.id.map))                 .getmap(); 

with

mmap = ((supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map)).getmap(); 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -