Problems with IntelliJ IDEA - incompatible types
When I try L开发者_如何学运维ocationProvider locationProvider = LocationManager.GPS_PROVIDER;
IntelliJ is giving me an error: incompatible types: required: android.location.LocationProvider found: java.lang.String.
What gives?
I have a project built in IDEA and haven't had this problem. Have you checked you are importing all the appropriate packages?
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.GpsStatus;
When you rightclick/Go To/Declaration LocationManager you should see the following code:
package android.location;
public class LocationManager {
public static final java.lang.String NETWORK_PROVIDER = "network";
public static final java.lang.String GPS_PROVIDER = "gps";
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 locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
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:
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
精彩评论