0

我正在使用 Google Play 服务来获取位置。

在我的测试中,有时,当我在不使用 WiFi 的情况下使用设备时,我会收到此错误。我不明白为什么有时它起作用,有时它不起作用。

09-25 14:53:45.389: E/AndroidRuntime(7732): FATAL EXCEPTION: main
09-25 14:53:45.389: E/AndroidRuntime(7732): java.lang.NullPointerException
09-25 14:53:45.389: E/AndroidRuntime(7732):     at bsfsdfgdfgzsdfgsfgsdfga.Helper.GPS.onConnected(GPS.java:63)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at com.google.android.gms.internal.u.v(Unknown Source)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at com.google.android.gms.internal.u$f.a(Unknown Source)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at com.google.android.gms.internal.u$f.a(Unknown Source)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at com.google.android.gms.internal.u$b.A(Unknown Source)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at com.google.android.gms.internal.u$a.handleMessage(Unknown Source)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at android.os.Looper.loop(Looper.java:130)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at android.app.ActivityThread.main(ActivityThread.java:3687)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at java.lang.reflect.Method.invokeNative(Native Method)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at java.lang.reflect.Method.invoke(Method.java:507)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
09-25 14:53:45.389: E/AndroidRuntime(7732):     at dalvik.system.NativeStart.main(Native Method)






public class GPS implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener,
    LocationListener, com.google.android.gms.location.LocationListener {


private LocationRequest lr;
private LocationClient lc;
static Location location;
double latitude = 0;
double longitude = 0;
Context context;
static Boolean status = false;

public  GPS(Context context) {
    this.context = context;
    lr = LocationRequest.create();
    lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    lr.setInterval(5000);       
    lr.setFastestInterval(1000);
    lc = new LocationClient(context, this, this);       
}


@Override
public void onLocationChanged(Location location) {  
    lc.removeLocationUpdates(this);
    lc.requestLocationUpdates(lr, this);

    location = lc.getLastLocation();
    latitude = location.getLatitude();
    longitude = location.getLongitude();

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {     
    Log.e("GPS", ""+arg0);

}       

@Override
public void onConnected(Bundle connectionHint) {
    Log.i("GPS", "Google Play Services Conectado.");
    lc.requestLocationUpdates(lr, this);

    location = lc.getLastLocation();
    latitude = location.getLatitude();
    longitude = location.getLongitude();        
}   

public double getLatitude() {
    return latitude;
}

public double getLongitude() {
    return longitude;
}   


public void connect(){
    lc.connect();
}

public void disconnect(){
    lc.disconnect();
}   


@Override
public void onDisconnected() {

}

public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

}

double latitude;
double longitude;
LocationClient lc;
LocationRequest lr;
Location location;
Button bt;
TextView tv;
GPS gps;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     bt = (Button)findViewById(R.id.bt);
     tv = (TextView)findViewById(R.id.tv);

     gps = new GPS(this);



     bt.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {                   
                latitude = gps.getLatitude();
                longitude = gps.getLongitude();
                TextView tv = (TextView)findViewById(R.id.tv);


                /*List<Address> ad = null;
                try {
                    Geocoder mGeocoder = new Geocoder(MainActivity.this);
                    ad =  mGeocoder.getFromLocation(latitude, longitude, 1);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                String ads = ""+ad.get(0).getAddressLine(0) + " "+ad.get(0).getAddressLine(1);
                */
                tv.setText(""+latitude + " "+longitude);
                Log.i("GPS", ""+latitude +  " "+longitude);

            }
        });  


}

@Override
protected void onStart() {
    gps.connect();  
    super.onStart();
}   

@Override
protected void onStop() {
    gps.disconnect();
    super.onStop();
}

@Override
protected void onPause() {
    gps.disconnect();
    super.onPause();
}

}

4

2 回答 2

2

例外是在第 63 行 @ GPS.java。您可能在该行错过了空指针检查。

getLastLocation()如果位置不可用,请参阅该方法可以返回 null。

换行

location = lc.getLastLocation();

location = lc.getLastLocation();
if(location != null){
    //do something with location
}
于 2013-09-25T18:13:05.187 回答
1

imranhasanhira 是正确的。也许您还可以检查是否至少有一个提供商可用于获取用户位置。你说当 WiFi 关闭时会发生崩溃,我想你的设备上的 GPS 和 SIM 卡都不是“活动的”。

在这里查看如何检查提供程序是否可用(如果任何提供程序可用,您也可以打开位置设置屏幕):Android 检测位置设置

于 2013-09-25T18:33:41.260 回答