我的地图出了问题。每当我尝试获取我的位置时它都会崩溃。当我使用我自己的坐标为我的位置时,正确显示和打开。每当我删除评论时,程序崩溃。 这是我的日志Cat。
01-26 13:34:49.609: E/dalvikvm(10290): Could not find class 'maps.ae.i', referenced from method maps.af.al.a
01-26 13:34:49.609: W/dalvikvm(10290): VFY: unable to resolve new-instance 5490 (Lmaps/ae/i;) in Lmaps/af/al;
01-26 13:34:49.609: D/dalvikvm(10290): VFY: replacing opcode 0x22 at 0x0091
01-26 13:34:49.632: D/dalvikvm(10290): VFY: dead code 0x0093-00a0 in Lmaps/af/al;.a (Landroid/view/LayoutInflater;Lcom/google/android/gms/maps/GoogleMapOptions;Z)Lmaps/af/al;
01-26 13:34:49.781: D/dalvikvm(10290): GC_CONCURRENT freed 343K, 41% free 4064K/6791K, external 598K/1110K, paused 3ms+5ms
01-26 13:34:50.070: D/dalvikvm(10290): GC_CONCURRENT freed 445K, 40% free 4283K/7111K, external 611K/1110K, paused 5ms+6ms
01-26 13:34:50.250: D/AndroidRuntime(10290): Shutting down VM
01-26 13:34:50.250: W/dalvikvm(10290): threadid=1: thread exiting with uncaught exception (group=0x40018578)
01-26 13:34:50.257: E/AndroidRuntime(10290): FATAL EXCEPTION: main
01-26 13:34:50.257: E/AndroidRuntime(10290): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pharmacyServices/com.example.pharmacyServices.Map}: java.lang.NullPointerException
01-26 13:34:50.257: E/AndroidRuntime(10290): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-26 13:34:50.257: E/AndroidRuntime(10290): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-26 13:34:50.257: E/AndroidRuntime(10290): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-26 13:34:50.257: E/AndroidRuntime(10290): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-26 13:34:50.257: E/AndroidRuntime(10290): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 13:34:50.257: E/AndroidRuntime(10290): at android.os.Looper.loop(Looper.java:130)
01-26 13:34:50.257: E/AndroidRuntime(10290): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-26 13:34:50.257: E/AndroidRuntime(10290): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 13:34:50.257: E/AndroidRuntime(10290): at java.lang.reflect.Method.invoke(Method.java:507)
01-26 13:34:50.257: E/AndroidRuntime(10290): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
代码:
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = lm.getBestProvider(criteria,true);
// Location location = lm.getLastKnownLocation(provider);
答案 0 :(得分:0)
尝试这样做
public class MyBestLocation {
LocationListener locationListenerGps = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
LocationListener locationListenerNetwork = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
//delay in seconds
private int DELAY = 1;
private Timer timer1;
private LocationManager lm;
private LocationResult locationResult;
private boolean gps_enabled = false;
private boolean network_enabled = false;
private ScheduledExecutorService scheduler;
private ScheduledFuture scheduledFuture;
public boolean getLocation(Context context, LocationResult result) {
// przypisanie referencji
locationResult = result;
if (lm == null)
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// sprawdzam dostepnosc providerow
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
// jesli zaden nie jest dostepny wylaczam sie
if (!gps_enabled && !network_enabled)
return false;
if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);
if (network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);
//----------------------
// async executor method upgrade
timer1 = new Timer();
timer1.schedule(new GetLastLocation(), DELAY*2000);
// ----------------
// opozniam uruchomienie watku w celu odszukania lokacji jesli ktorys z
// providerow zdola to zrobic
/* scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(new GetLastLocation(), DELAY, TimeUnit.SECONDS);*/
return true;
}
// Timer task sluzy do odszukania najmlodszej lokalizacji
public static abstract class LocationResult {
public abstract void gotLocation(Location location);
}
/**
* Klasa pomocnicza odpalana przez TimerTask.
*
* @author rafal.machnik
*/
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc = null, gps_loc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// if there are both values use the latest one
if (gps_loc != null && net_loc != null) {
if (gps_loc.getTime() > net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if (gps_loc != null) {
locationResult.gotLocation(gps_loc);
return;
}
if (net_loc != null) {
locationResult.gotLocation(net_loc);
return;
}
// locationResult.gotLocation(null);
}
}
要调用它,你只能实现那样的
MyBestLocation bestLocation = new MyBestLocation();
if (bestLocation.getLocation(EditTask.this, locationResult)) {
locationResult.gotLocation(location);
}