android - mapview.getoverlays() null pointer exception -


hi trying implement overlay onto google map. receiving fatal error @ runtime , seems sort of nullpointerexception. app runs when remove "overlaylist = mapview.getoverlays();" , "overlaylist.add(t);" lines of code. appreciated!

main activity:

public class csactivity extends mapactivity implements locationlistener  {   mapcontroller mapcontroller;  mapview mapview;  locationmanager locationmanager;  mylocationoverlay mylocationoverlay; mylocationoverlay compass;  private button click_but; private button exit_but; long start; long stop; int x,y; geopoint touchedpoint; drawable d; list<overlay> overlaylist; string towers; int lat; int lng;  public void oncreate(bundle bundle) {     super.oncreate(bundle);     setcontentview(r.layout.main); // bind layout activity      click_but = new button(this);     click_but = (button)findviewbyid(r.id.clickbtn);     exit_but = new button(this);     exit_but = (button)findviewbyid(r.id.exitbtn);      d = getresources().getdrawable(r.drawable.markerblue);     touchy t = new touchy();      overlaylist = mapview.getoverlays();     overlaylist.add(t);     /*compass = new mylocationoverlay(csactivity.this, mapview);     overlaylist.add(compass);*/       // configure map     mapview = (mapview) findviewbyid(r.id.mapview);     mapview.setbuiltinzoomcontrols(true);     mapview.setsatellite(true);     mapcontroller = mapview.getcontroller();     mapcontroller.setzoom(20); // zoom 1 world view     locationmanager = (locationmanager) getsystemservice(context.location_service);        mylocationoverlay = new mylocationoverlay(this, mapview);     mapview.getoverlays().add(mylocationoverlay);       criteria crit = new criteria();     towers = locationmanager.getbestprovider(crit, false);     location l = locationmanager.getlastknownlocation(towers);     if (l != null){         lat = (int) (l.getlatitude()*1e6);         lng = (int) (l.getlongitude()*1e6);          geopoint ourlocation = new geopoint(lat, lng);         overlayitem overlayitem = new overlayitem(ourlocation, "what's up", "2nd string");         myitemizedoverlay custom = new myitemizedoverlay(d, csactivity.this);         custom.insertpinpoint(overlayitem);         overlaylist.add(custom);     }else{         toast.maketext(csactivity.this, "couldn't provider", toast.length_short).show();     }         final csactivity home = this;         exit_but.setonclicklistener(new onclicklistener(){          @override         public void onclick(view v)          {             intent intent = new intent();             intent.setclass(home, homeactivity.class);             startactivity(intent);          }      });      click_but.setonclicklistener(new onclicklistener(){          @override         public void onclick(view v)          {           }      });          }  @override protected boolean isroutedisplayed() {     return false; }    @override protected void onresume() {     super.onresume();     mylocationoverlay.enablemylocation();     mylocationoverlay.enablecompass();     locationmanager.requestlocationupdates(towers, 500, 1, this);  }  @override protected void onpause() {     super.onresume();     mylocationoverlay.disablemylocation();     mylocationoverlay.disablecompass();     locationmanager.removeupdates(this); }  class touchy extends overlay {     public boolean ontouchevent(motionevent e, mapview m){         if (e.getaction() == motionevent.action_down){             start = e.geteventtime();             x = (int) e.getx();             y = (int) e.gety();              touchedpoint = mapview.getprojection().frompixels(x, y);         }         if (e.getaction() == motionevent.action_up){             stop = e.geteventtime();         }         if (stop - start > 1000){             //perform action             alertdialog alert = new alertdialog.builder(csactivity.this).create();             alert.settitle("pick option");             alert.setmessage("i said pick!");             alert.setbutton("place pinpoint", new dialoginterface.onclicklistener() {                  @override                 public void onclick(dialoginterface dialog, int which) {                     // todo auto-generated method stub                      overlayitem overlayitem = new overlayitem(touchedpoint, "what's up", "2nd string");                     myitemizedoverlay custom = new myitemizedoverlay(d, csactivity.this);                     custom.insertpinpoint(overlayitem);                     overlaylist.add(custom);                 }             });      alert.setbutton2("get address", new dialoginterface.onclicklistener() {                  @override                 public void onclick(dialoginterface dialog, int which) {                     // todo auto-generated method stub                      geocoder geocoder = new geocoder(getbasecontext(), locale.getdefault());                     try{                         list<address> address = geocoder.getfromlocation(touchedpoint.getlatitudee6() / 1e6, touchedpoint.getlongitudee6() / 1e6, 1);                     if (address.size() > 0){                         string display = "";                         (int = 0; i<address.get(0).getmaxaddresslineindex(); i++){                              display += address.get(0).getaddressline(i) + "\n";                         }                         toast t = toast.maketext(getbasecontext(), display, toast.length_long);                         t.show();                     }                     } catch (ioexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     } finally{                      }                 }             });             alert.show();             return true;          }           return false;     } }           @override public void onlocationchanged(location location) {     // todo auto-generated method stub     lat = (int) (location.getlatitude() *1e6);     lng = (int) (location.getlongitude()*1e6);  }  @override public void onproviderdisabled(string provider) {     // todo auto-generated method stub  }  @override public void onproviderenabled(string provider) {     // todo auto-generated method stub  }  @override public void onstatuschanged(string provider, int status, bundle extras) {     // todo auto-generated method stub  } } 

itemized overlay activity:

   public class myitemizedoverlay extends itemizedoverlay<overlayitem>  {  private arraylist<overlayitem> pinpoints = new arraylist<overlayitem>();  private context c;  public myitemizedoverlay(drawable defaultmarker) {     super(boundcenter(defaultmarker));     // todo auto-generated constructor stub }  public myitemizedoverlay(drawable m, context context) {     // todo auto-generated constructor stub     this(m);     c = context; }  @override protected overlayitem createitem(int i) {     // todo auto-generated method stub     return pinpoints.get(i); }  @override public int size() {     // todo auto-generated method stub     return pinpoints.size(); }  public void insertpinpoint(overlayitem item) {     pinpoints.add(item);     this.populate(); }  } 

main xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" >  <com.google.android.maps.mapview     android:id="@+id/mapview"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_centerinparent="true"     android:apikey="(key here)"     android:clickable="true"     android:enabled="true" />  <button     android:id="@+id/clickbtn"     style="@style/buttontext"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_alignparentright="true"     android:background="@drawable/blue"     android:text="click" />  <button     android:id="@+id/exitbtn"     style="@style/buttontext"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_alignparentleft="true"     android:background="@drawable/red"     android:text="home" />  </relativelayout> 

mapview null npe first give refrences xml file

mapview = (mapview) findviewbyid(r.id.mapview);  

then overlays

overlaylist = mapview.getoverlays(); 

and

if(null!=overlaylist&&overlaylist.size()!=0){ overlaylist.add(t); } 

why nullpointerexception ?


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -