Getlastknowlocation NULL pointer exception in Android using Google map API

Source: Internet
Author: User

This problem has also plagued me for a long time. Why is it depressing that I used a simulator to test whether it was successful or failed?

First, you should remember the role of the following code:

// Set service provider Information <br/> Criteria = new criteria (); <br/> // standard of service accuracy <br/> criteria. setaccuracy (criteria. accuracy_fine); <br/> // No Height Information <br/> criteria. setaltituderequired (false); <br/> // No Location Information <br/> criteria. setbearingrequired (false); <br/> // no fee is allowed <br/> criteria. setcostallowed (false); <br/> // Low Power Consumption <br/> criteria. setpowerrequirement (criteria. power_low); <br/> // obtain the most matched criteria <br/> // best_provider = mlocationmanager. getbestprovider (criteria, true ); 

Through the above program, we obtained the best service provision method, but after many experiments, I found that the best provider obtained each time is the GPS method, this means that the GPS method is used to obtain the location after the operation. Then, we usually obtain the location as follows:

// Obtain the obtained coordinate information. <br/> mlocation = mlocationmanager. getlastknownlocation (best_provider); <br/> // get the address code, used to find the current address name later <br/> mgeocoder = new geocoder (this, locale. getdefault (); <br/> // update Location Information <br/> updatelocation (mlocation); <br/> // create a locationmanager object and listen for location changes, and update mapview <br/> mlocationmanager. requestlocationupdates (best_provider, 6000,100, mlocationlistener );

// Update location <br/> Public void updatelocation (location) <br/>{< br/> If (location = NULL) <br/>{< br/> return; <br/> // mlocationmanager. requestlocationupdates (best_provider, 60000,1, mlocationlistener); <br/>}< br/> // you can use locationoverlay to draw the current location. <br/> mlocationoverlay = new locationoverlay (this ); <br/> List <overlay> overlays = mmapview. getoverlays (); <br/> overlays. add (mlocationoverlay); <br/> // draw the current location <br/> mlocationoverlay. setlocation (location); <br/> // current latitude and longitude <br/> double geolat = location. getlatitude () * 1e6; <br/> double geolng = location. getlongpolling () * 1e6; <br/> // convert it to geopoint type <br/> geopoint point = new geopoint (geolat. intvalue (), geolng. intvalue (); <br/> // dynamically locates the specified coordinate. <br/> mmapcontroller. animateto (point); <br/>}</P> <p> // set the location listener <br/> private final locationlistener mlocationlistener = new locationlistener () <br/>{< br/> // update when the location changes <br/> Public void onlocationchanged (location) <br/>{< br/> updatelocation (location); <br/>}< br/> // when the provider is unavailable, for example, GPS is not enabled <br/> Public void onproviderdisabled (string provider) <br/>{< br/> updatelocation (null ); <br/>}< br/> // when the provider is enabled, for example, if GPS is enabled <br/> Public void onproviderenabled (string provider) {}< br/> // when the provider's status changes, for example, if yes, it cannot be used for the time being. When switching between switches is disabled <br/> Public void onstatuschanged (string provider, int status, bundle extras) {}< br/> }; 

Pay attention to the getlastknownlocation method above. This method means to obtain the last known location. The problem here is that if we start this program for the first time, what is the last location? I am also wondering

We know that in the simulator, commands such as GEO fix 122 22 are used to simulate the current position. I found that every time I simulate a location, an error occurs when I start the program for the first time, the location is obtained only after startup. This may be the disadvantage of the method. I don't know if I understand the error.

 

So test it on a real machine. Can this happen? Unfortunately, when GPS is used to locate a real machine program, the GPS module is enabled, and other positioning software can be used to locate the current location, however, the program you write cannot obtain the location.

Suggestions from netizens are as follows:

While (location = NULL) <br/>{< br/> mlocationmanager. requestlocationupdates ("best_provider", 60000, 1, locationlistener); <br/>} 

 

Some netizens wrote the following program to replace the above loop:

/** <Br/> * here, you must re-set and listen to the locationmanager. <br/> * the process of obtaining the provider from MGR is not one time. <br/> * Mgr. getlastknownlocation is likely to return NULL <br/> * if it is only in initprovider () registering a listener in <br/> */<br/> @ override <br/> protected void onresume () {<br/> super. onresume (); <br/> Mgr. requestlocationupdates (bundle. getstring ("provider"), 60000, 1, locationlistener); <br/>}</P> <p> @ override <br/> protected void onpause () {<br/> super. onpause (); <br/> Mgr. removeupdates (locationlistener); <br/>} 

The above program constantly judges whether the location is null until the location is obtained. The explanation is that getlastknownlocation cannot be obtained at one time, and it must be obtained multiple times, this just verifies my conjecture that it cannot be successfully started for the first time. However, I did not get any position except the loop.

 

The filter condition provided by the previous service makes the result obtained by the location provider GPS service every time I obtain the location provider, however, this method is not very good. Later, I simply wrote the service access method as getting it through the network.

Best_provider = mlocationmanager. network_provider; <br/> // obtain the obtained coordinate information. <br/> mlocation = mlocationmanager. getlastknownlocation (best_provider ); 

It turns out that the problem lies in the location service acquisition method. After using the network method, you can get the current location on the real machine.

 

Then the program I wrote can finally run on the real machine. paste a picture to see it.

The above text shows that the address is not found because of the network speed problem. I should have found the relevant location information through address encoding. This is a small program I developed using Google map API, after completing some basic map functions, you can make a lot of extensions on it. Currently, the LBS service is very popular, such as UC Park, public comments, Netease eight directions, and the street, there is also the famous Foursquare outside China, hoping to learn and communicate with friends who are interested in this field.

 

The following is an example of how to use getlastknownlocation from a foreign website:

 

The call to request update for a location is not blocking, hence it wont wait there. Also the provider in emulator may not have been started.

A possible check cocould be to see if the settings in it disable GPS provider? Then send GEO fix.

However, I wocould use location listener, it wocould be ideal in your case since you need a GEO fix to proceed further. location listener is used for sorting configurations from the locationmanager when the location has changed. you can unregister the listener
After first geofix.

Note: it can take some time on device to get current location, and even on device this can return null.

 

 

The following are two webpages that describe this problem abroad:

Http://marakana.com/forums/android/examples/42.html

Http://stackoverflow.com/questions/1608632/android-locationmanager-getlastknownlocation-returns-null

 

 

Related Article

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.