Baidu Map Positioning Java.lang.NullPointerException

Source: Internet
Author: User

The whole four or five days of showing the map is finally not Beijing, sad tears a t~t.

Java.lang.NullPointerException is a null pointer exception, greet the degree Niang, and see a few times their code did not feel that there is an anomaly, the whim of the change code unexpectedly became ...

(Here is the code after the change)

1  Public classLocal_mainactivity extends Activity {2     PrivateMapview Mapview;3     PrivateBaidumap Baidumap;4     PrivateLocationmanager Locationmanager;5     PrivateString provider;6     PrivateBoolean isfristloacte=true;7 8 @Override9     protected voidonCreate (Bundle savedinstancestate) {Ten super.oncreate (savedinstancestate); One sdkinitializer.initialize (Getapplicationcontext ()); A Setcontentview (r.layout.local); -  - //Baidu Map the  -          This. Mapview =(Mapview) Findviewbyid (R.id.map_view); -          This. Baidumap = This. Mapview.getmap (); -          This. locationmanager=(Locationmanager) Getsystemservice (context.location_service); +         //get all available address providers -List<string>providerlist; +providerlist= This. Locationmanager.getproviders (true); A          if(Providerlist.contains (Locationmanager.network_provider)) { atProvider =Locationmanager.network_provider; -System. out. println (); -System. out. println ("Network!!!!!!!!!!!!!!!!!"); -}Else if(Providerlist.contains (Locationmanager.gps_provider)) { -               This. Provider =Locationmanager.gps_provider; -System. out. println (); inSystem. out. println ("GPS!!!!!!!!!!!!!!!!!"); -          } to         Else { +Toast.maketext ( This,"No Location Provider", Toast.length_short). Show (); -             return; the         } *  $         Try  {Panax NotoginsengLocation location = This. Locationmanager.getlastknownlocation ( This. Provider); -           //Location = this.locationManager.getLastKnownLocation (This.locationManager.GPS_PROVIDER); the             if(Location! =NULL) { + NavigateTo (location); A             } the               This. locationmanager.requestlocationupdates (Provider, the,1, Locationlistener); +}Catch(Exception e) { -System. out. println (e); $         } $  -     } -   /*@Override the protected void OnDestroy () { - Super.ondestroy (); FFFFRWuyi Mapview.ondestroy (); the     } - @Override Wu protected void OnPause () { - super.onpause (); About mapview.onpause (); $     } - @Override - protected void Onresume () { - Super.onresume (); A Mapview.onresume (); +     }*/ the  -   Private voidNavigateTo (location location) { $       if(isfristloacte) { theLATLNG ll=Newlatlng (Location.getlatitude (), Location.getlongitude ()); theMapstatusupdate update=MAPSTATUSUPDATEFACTORY.NEWLATLNG (ll); the baidumap.animatemapstatus (update); theUpdate=Mapstatusupdatefactory.zoomto (16f); - baidumap.animatemapstatus (update); inIsfristloacte=false; the       } the   } About  the @Override the      Publicboolean Oncreateoptionsmenu (Menu menu) { the         //inflate the menu; This adds items to the action bar if it is present. + getmenuinflater (). Inflate (R.menu.menu_main, menu); -         return true; the     }Bayi  the @Override the      Publicboolean onoptionsitemselected (MenuItem item) { -         //Handle Action Bar item clicks here. The Action Bar would -         //automatically handle clicks on the Home/up button, so long the         //As you specify a the parent activity in Androidmanifest.xml. the         intID =Item.getitemid (); the  the         //noinspection simplifiableifstatement -         if(id = =r.id.action_settings) { the             return true; the         } the 94         returnsuper.onoptionsitemselected (item); the     } theLocationlistener locationlistener=NewLocationlistener () { the @Override98          Public voidonlocationchanged (location location) { About             if(location!=NULL){ - NavigateTo (location);101             }102         }103 104 @Override the          Public voidOnstatuschanged (String provider,intstatus, Bundle extras) {106 107         }108 109 @Override the          Public voidonproviderenabled (String provider) {111  the         }113  the @Override the          Public voidonproviderdisabled (String provider) { the 117         }118     };119 @Override -     protected voidOnDestroy () {121 Super.ondestroy ();122 Mapview.ondestroy ();123         if( This. locationmanager!=NULL){124             Try{ the                  This. Locationmanager.removeupdates (Locationlistener);126}Catch(Exception ex) {}127  -         }129     } the @Override131     protected voidOnPause () { the super.onpause ();133 mapview.onpause ();134     }135 @Override136     protected voidOnresume () {137 Super.onresume ();138 Mapview.onresume ();139     } $ 141}

That's because 22-33 of the code is written like this.

              
if (Providerlist.contains (Locationmanager.gps_provider)) {               this.provider = Locationmanager.gps_provider;               System.out.println ();               System.out.println ("GPS!!!!!!!!!!!!!!!!!");          } else if (Providerlist.contains (Locationmanager.network_provider)) {
              Provider = Locationmanager.network_provider;              System.out. println ();              SYSTEM.OUT.PRINTLN ("Network!!!!!!!!!!!!!!!!!" );              else {toast.maketext (this, "No provider-to-use", Toast.length_short). Show (); Return  }

Don't ask me why
It's okay anyway.

Baidu Map Positioning Java.lang.NullPointerException

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.