When I try LocationProvider locationProvider = LocationManager.GPS_PROVIDER; IntelliJ is giving me an error: incompatible types: required: android.location.LocationProvider found: java.lang.String.
What gives?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I have a project built in IDEA and haven’t had this problem. Have you checked you are importing all the appropriate packages?
When you rightclick/Go To/Declaration LocationManager you should see the following code:
Update
Ok. I know what is going on here. What you are doing is assigning a LocationManager.GPS_PROVIDER (which is actually a string) to a LocationProvider object. hence the incompatible types error.
Is there a reason you want to instantiate a LocationProvider object? Looking at the LocationManager API, you don’t actually need one of these. The API takes the constant strings (GPS_PROVIDER, NETWORK_PROVIDER etc) and instantiates the appropriate LocationProvider internally. If you really need the LocationProvider instance, you can get it back from the LocationManager with the getProvider method.
Another Update
So it looks like the reason the OP encounted this problem in the first place is that the sample code on the Obtaining User Location documentation is just wrong:
LocationProvider is an abstract superclass and you shouldn’t be instantiating these things directly. Instead you pass a LocationManager.type_of_provider_here into the LocationManager and it instantiates an object that implements that abstract class. So the sample should be: