2013-05-31 25 views
16

Tôi đang tái chế mã được cung cấp để kiểm tra xem thiết bị của người dùng có dịch vụ Google Play trên thiết bị đó trước khi có được dữ liệu vị trí từ http://developer.android.com/training/location/retrieve-current.html hay không. Khi sao chép dán nó vào IDE của tôi, Eclipse một cách chính xác chỉ ra sai sót trong dòng, bởi vì "connectionResult" chưa bao giờ được xác định, cũng không phải là "getSupportFragmentManager"Hướng dẫn truy xuất vị trí hiện tại trên Android chưa rõ ràng

int errorCode = connectionResult.getErrorCode(); 

errorFragment.show(getSupportFragmentManager(), 
        "Location Updates"); 

Tôi có nên chỉ cần tạo một biến ở trên được gọi là ConnectionResult connectionResult để khắc phục sự cố? Tôi không chắc chắn về cách sửa lỗi thứ hai.

Bên cạnh đó, dòng

mLocationClient = new LocationClient(this, this, this); 

từ hơn nữa cùng trang đề nghị đưa vào các lớp MainActivity mà không đáp ứng các constructor LocationClient, gợi lỗi khác.

Cập nhật: Một vấn đề khác với hướng dẫn. Xin chào, bài hướng dẫn tham khảo lớp LocationResult mà nó chưa tạo ở đây: http://developer.android.com/training/location/receive-location-updates.html. Làm thế nào/nơi tôi có nghĩa vụ phải xác định điều này?

+1

tiêu đề của bạn không liên quan đến mô tả của bạn – juned

+0

@juned cảm ơn bạn, tôi hy vọng nó là rõ ràng hơn bây giờ – NumenorForLife

Trả lời

35

Hướng dẫn gây hiểu lầm. Nếu bạn muốn kiểm tra các dịch vụ google play tồn tại thì hãy làm như sau.

int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
if (errorCode != ConnectionResult.SUCCESS) { 
    GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show(); 
} 

Điều này sẽ tự động hiển thị hộp thoại lỗi thích hợp nếu nó không tồn tại.

Đến vấn đề thứ hai của bạn. Phần còn lại của hướng dẫn này cần phải được theo sau. Bạn không cần phải thực hiện GooglePlayServicesClient.ConnectionCallbacksGooglePlayServicesClient.OnConnectionFailedListener nếu bạn muốn tạo ra các locationclient sử dụng new LocationClient(this, this, this);

Lưu ý: không nên cố gắng sử dụng locationclient cho đến sau khi phương pháp onConnected được gọi callback của bạn.

-1

Làm theo hướng dẫn tôi chạy vào cùng một lỗi, tuy nhiên mẫu mã được cung cấp có vẻ được triển khai chính xác.

/** 
* Verify that Google Play services is available before making a request. 
* 
* @return true if Google Play services is available, otherwise false 
*/ 
private boolean servicesConnected() { 

    // Check that Google Play services is available 
    int resultCode = 
      GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 

    // If Google Play services is available 
    if (ConnectionResult.SUCCESS == resultCode) { 
     // In debug mode, log the status 
     Log.d(LocationUtils.APPTAG, getString(R.string.play_services_available)); 

     // Continue 
     return true; 
    // Google Play services was not available for some reason 
    } else { 
     // Display an error dialog 
     Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0); 
     if (dialog != null) { 
      ErrorDialogFragment errorFragment = new ErrorDialogFragment(); 
      errorFragment.setDialog(dialog); 
      errorFragment.show(getSupportFragmentManager(), LocationUtils.APPTAG); 
     } 
     return false; 
    } 
} 

http://developer.android.com/shareables/training/LocationUpdates.zip

Các vấn đề liên quan