java - How to add map to a tabbed activity (Android) -


i have tabbed activity uses sectionspageadapter. there 2 tabs, each uses different fragment, 1 of google map. when attempt supportmapfragment mapfragment returns null.

supportmapfragment mapfragment = (supportmapfragment) getchildfragmentmanager()                 .findfragmentbyid(r.id.map); 

i have tried:

supportmapfragment mapfragment = (supportmapfragment) getfragmentmanager()                 .findfragmentbyid(r.id.map); 

if create new googlemapsactivity works fine, confirms has nothing key. fragment id map exists.

mainactivity.java

package com.example.frias19o.trackthem2;  import android.support.design.widget.tablayout; 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.support.v4.view.viewpager; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view;  public class mainactivity extends appcompatactivity {      private sectionspageradapter msectionspageradapter;      private viewpager mviewpager;      @override     protected void oncreate(bundle savedinstancestate)  {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);         // create adapter return fragment each of 3         // primary sections of activity.         msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager());          // set viewpager sections adapter.         mviewpager = (viewpager) findviewbyid(r.id.container);         mviewpager.setadapter(msectionspageradapter);          tablayout tablayout = (tablayout) findviewbyid(r.id.tabs);         tablayout.setupwithviewpager(mviewpager);          floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab);         fab.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 snackbar.make(view, "replace own action", snackbar.length_long)                         .setaction("action", null).show();             }         });     }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_map, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }  } 

mapfragment.java

package com.example.frias19o.trackthem2;  import android.content.intent; import android.os.bundle; import android.provider.settings; import android.support.annotation.nonnull; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.toast;  import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.location.locationservices; 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;  import org.json.jsonexception; import org.json.jsonobject;  /********************************************************************  * fragment map  *********************************************************************/ public class mapfragment extends fragment implements onmapreadycallback {     private static final string logtag = "mapfragment";     private static final string arg_section_number = "section_number";     public static googleapiclient mgoogleapiclient;     private googlemap mmap;      public mapfragment() {     }      public static mapfragment newinstance(int sectionnumber) {         mapfragment fragment = new mapfragment();         bundle args = new bundle();         args.putint(arg_section_number, sectionnumber);         fragment.setarguments(args);         return fragment;     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view rootview;         toast.maketext(getcontext(), "fragment_map", toast.length_short).show();         rootview = inflater.inflate(r.layout.activity_maps, container, false);          return rootview;     }      @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);          supportmapfragment mapfragment = (supportmapfragment) getfragmentmanager()                 .findfragmentbyid(r.id.map);         mapfragment.getmapasync(this); // brakes here because mapfragment null     }       @override     public void onmapready(googlemap googlemap) {         mmap = googlemap;         log.v(logtag, "map ready");          latlng sydney = new latlng(-34, 151);         mmap.addmarker(new markeroptions().position(sydney).title("marker in sydney"));         mmap.movecamera(cameraupdatefactory.newlatlng(sydney));         log.v(logtag, "added sydney");     } } 

sectionspageradapter.java

package com.example.frias19o.trackthem2;  import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter;  /***********************************************************************  * {@link fragmentpageradapter} returns fragment corresponding  * 1 of sections/tabs/pages.  **********************************************************************/ public class sectionspageradapter extends fragmentpageradapter {      public sectionspageradapter(fragmentmanager fm) {         super(fm);     }      @override     public fragment getitem(int position) {         // getitem called instantiate fragment given page.         // return clusterfragment (defined static inner class below).          fragment fragment = null;          switch (position) {             case 0:                 fragment = mapfragment.newinstance(position + 1);                 break;             case 1:                 fragment = clusterfragment.newinstance(position + 1);                 break;             default:                 break;         }          return fragment;     }      @override     public int getcount() {         // show 2 total pages.         return 2;     }      @override     public charsequence getpagetitle(int position) {         switch (position) {             case 0:                 return "section 1";             case 1:                 return "section 2";         }         return null;     } } 

note: code on link provided coomonsware in first comment on question, led me solution.

as pointed out commonsware, solution making mapfragment extend supportmapfragment, way getmapasync(this); can called directly (without having access fragmentmanager). return instance of mapfragment in getitem() of sectionspageradapter. note there should not call/override oncreateview(), caused java.lang.nullpointerexception: attempt invoke interface method 'void maps.ei.bz.o()' on null object reference

final product:

mapfragment.java

package com.example.frias19o.trackthem2;  import android.os.bundle; import android.support.annotation.nullable; import android.util.log; import com.google.android.gms.common.api.googleapiclient; 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;  /********************************************************************  * fragment map  *********************************************************************/ public class mapfragment extends supportmapfragment implements onmapreadycallback {     private static final string logtag = "mapfragment";     private googlemap mmap;      public mapfragment() {     }      @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);        getmapasync(this);     }       @override     public void onmapready(googlemap googlemap) {         mmap = googlemap;         log.v(logtag, "map ready");          latlng sydney = new latlng(-34, 151);         mmap.addmarker(new markeroptions().position(sydney).title("marker in sydney"));         mmap.movecamera(cameraupdatefactory.newlatlng(sydney));         log.v(logtag, "added sydney");     } } 

sectionspageradapter.java

package com.example.frias19o.trackthem2;  import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter;  /***********************************************************************  * {@link fragmentpageradapter} returns fragment corresponding  * 1 of sections/tabs/pages.  **********************************************************************/ public class sectionspageradapter extends fragmentpageradapter {      public sectionspageradapter(fragmentmanager fm) {         super(fm);     }      @override     public fragment getitem(int position) {         // getitem called instantiate fragment given page.         // return clusterfragment (defined static inner class below).          fragment fragment = null;          switch (position) {             case 0:                 fragment = new mapfragment();                  break;             case 1:                 fragment = clusterfragment.newinstance(position + 1);                 break;             default:                 break;         }          return fragment;     }      @override     public int getcount() {         // show 2 total pages.         return 2;     }      @override     public charsequence getpagetitle(int position) {         switch (position) {             case 0:                 return "section 1";             case 1:                 return "section 2";         }         return null;     } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -