我从Google Play服务收到错误消息。它出什么问题了?

时间:2013-09-25 18:03:13

标签: android gps location google-play-services

我正在使用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();
}

}

2 个答案:

答案 0 :(得分:2)

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

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

更改行

location = lc.getLastLocation();

location = lc.getLastLocation();
if(location != null){
    //do something with location
}

答案 1 :(得分:1)

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

在此处查看如何检查提供商是否可用(如果有任何提供商可用,您也可以打开位置设置屏幕):Android detect location settings