android - SupportMapFragment.getMap() on a null object reference -


after trying everything, cannot seem getmap without pulling null object reference. trying inflate google mapfragment fragment, each time keep getmap null object .

here code

i'm desperate @ point, i've tried everything.

error

  java.lang.nullpointerexception: attempt invoke virtual method 'com.google.android.gms.maps.googlemap com.google.android.gms.maps.supportmapfragment.getmap()' on null object reference             @ com.gnumbu.errolgreen.importedapplication.viewmapfragment.onviewcreated(viewmapfragment.java:122)             @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:971)             @ 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.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141)             @ android.support.v4.view.viewpager.populate(viewpager.java:1073)             @ android.support.v4.view.viewpager.populate(viewpager.java:919)             @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1441)             @ android.view.view.measure(view.java:17430)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463)             @ android.widget.framelayout.onmeasure(framelayout.java:430)             @ android.view.view.measure(view.java:17430)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463)             @ android.support.v7.internal.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:453)             @ android.view.view.measure(view.java:17430)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463)             @ android.widget.framelayout.onmeasure(framelayout.java:430)             @ android.view.view.measure(view.java:17430)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463)             @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436)             @ android.widget.linearlayout.measurevertical(linearlayout.java:722)             @ android.widget.linearlayout.onmeasure(linearlayout.java:613)             @ android.view.view.measure(view.java:17430)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463)             @ android.widget.framelayout.onmeasure(framelayout.java:430)             @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2560)             @ android.view.view.measure(view.java:17430)             @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2001)             @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1166)             @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1372)             @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1054)             @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5779)             @ android.view.choreographer$callbackrecord.run(choreographer.java:767)             @ android.view.choreographer.docallbacks(choreographer.java:580)             @ android.view.choreographer.doframe(choreographer.java:550)             @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:753)             @ 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:5221)             @ java.lang.reflect.method.invoke(native method)             @ java.lang.reflect.method.invoke(method.java:372)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 

fragment java class

public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         if (container == null) {             return null;         }         view = (relativelayout) inflater.inflate(r.layout.fragmentmenu_layout, container, false);         // passing harcoded values latitude & longitude. please change per need. used drop marker on map         latitude = 26.78;         longitude = 72.56;        ; // setting mapfragment          return view;     }      /***** sets map if possible *****/     public static void setupmapifneeded() {         // null check confirm have not instantiated map.         if (mmap == null) {             // try obtain map supportmapfragment.             mmap = ((supportmapfragment) sectionspageradapter.fragmentmanager                     .findfragmentbyid(r.id.mapview)).getmap();             // check if successful in obtaining map.             if (mmap != null)                 setupmap();         }     }      /**      * can add markers or lines, add listeners or move      * camera.      * <p>      * should called once , when sure {@link #mmap}      * not null.      */     private static void setupmap() {         // showing move loction button         mmap.setmylocationenabled(true);         // dropping marker @ point on map         mmap.addmarker(new markeroptions().position(new latlng(latitude, longitude)).title("my home").snippet("home address"));         // zooming automatically dropped pin location         mmap.animatecamera(cameraupdatefactory.newlatlngzoom(new latlng(latitude,                 longitude), 12.0f));      }      @override     public void onviewcreated(view view, bundle savedinstancestate) {         // todo auto-generated method stub         if (mmap != null)             setupmap();          if (mmap == null) {             // try obtain map supportmapfragment.             mmap = ((supportmapfragment) mainactivity.fragmentmanager                     .findfragmentbyid(r.id.mapview)).getmap(); // getmap deprecated             // check if successful in obtaining map.             if (mmap != null)                 setupmap();             getallfeatureditems();             capturemapscreen();         }     }      @override     public void onstart() {         super.onstart();     }      @override     public void onresume() {         super.onresume();         setupmapifneeded();     }      @override     public void onpause() {         super.onpause();     }      @override     public void onstop() {         super.onstop();     }      @override     public void onlowmemory() {         super.onlowmemory();     }      /**** mapfragment's id must removed fragmentmanager      **** or else if same passed on next time      **** app crash ****/     @override     public void ondestroyview() {         super.ondestroyview();         if (mmap != null) {             mainactivity.fragmentmanager.begintransaction()                     .remove(mainactivity.fragmentmanager.findfragmentbyid(r.id.mapview)).commit();             mmap = null;         }     }      @override     public void ondestroy() {         super.ondestroy();     } 

fragmentmenu_layout.xml

 <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent"     android:translationz="5dp" >       <listview         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/listviewmenu"         android:layout_gravity="center_horizontal"         android:dividerheight="1sp"         android:translationz="5dp"         android:divider="@color/background_default"         android:background="@color/background_white"         android:clickable="true"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true"         android:layout_below="@+id/screenshotmapview" />      <imageview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/screenshotmapview"         android:layout_alignparenttop="true"         android:maxwidth="50dp"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true"         android:layout_alignparentright="true"         android:layout_alignparentend="true"         android:adjustviewbounds="true"         android:translationz="5dp"     />            <fragment             class="com.google.android.gms.maps.mapfragment"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/mapview"             android:layout_alignparenttop="true"             android:layout_alignparentright="true"             android:layout_alignparentend="true"             android:layout_alignparentleft="true"             android:layout_alignparentstart="true"             android:adjustviewbounds="true"             android:longclickable="false"             android:visibility="visible"             android:maxheight="50dp"             android:maxwidth="50dp"          />       </relativelayout> 

mainactivity.java

public static fragmentmanager fragmentmanager;        @override     protected void oncreate(bundle savedinstancestate) {        // settheme(r.style.actionbartheme);         super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_main);          // initialising object of fragmentmanager. here i'm passing getsupportfragmentmanager(). can pass getfragmentmanager() if coding android 3.0 or above.       //    final actionbar actionbar = getsupportactionbar();         buildgoogleapiclient();   //       actionbar.setnavigationmode(actionbar.navigation_mode_tabs);  //       actionbar.setnavigationmode(actionbar.navigation_mode_tabs);          mimagebitmap = null;          // create adapter return fragment each section/page         msectionspageradapter = new sectionspageradapter(                 this.getbasecontext(), getsupportfragmentmanager());         fragmentmanager = getsupportfragmentmanager();         // set viewpager sections adapter.         mviewpager = (viewpager) findviewbyid(r.id.pager);          mviewpager.setadapter(msectionspageradapter);          mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() {             @override             public void onpageselected(int position) {       //          actionbar.setselectednavigationitem(position);             }         });          // each of sections in app, add tab action bar.         /*         (int = 0; < msectionspageradapter.getcount(); i++) {             // create tab text corresponding page title defined             // adapter. specify activity object, implements             // tablistener interface, callback (listener) when             // tab selected.                      if (i == 0) {                         actionbar.addtab(                                 actionbar.newtab()                                          .seticon(r.drawable.ic_action_view_as_list)                                        // .settext(msectionspageradapter.getpagetitle(i))                                         .settablistener(this)                         );                     }                     if (i == 1) {                         actionbar.addtab(                                   actionbar.newtab()                                  .seticon(r.drawable.ic_action_web_site_dark)                                // .settext(msectionspageradapter.getpagetitle(i))                                 .settablistener(this)                 );             }         }         */         malbumstoragedirfactory = new basealbumdirfactory();           intent intent = new intent(this, splashscreen.class);         startactivity(intent);  } 

not sure why downvoted on this, have solved issue.

this line

mmap = ((supportmapfragment) mainactivity.fragmentmanager                     .findfragmentbyid(r.id.mapview)).getmap(); 

needed changed

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

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -