2013-07-23 24 views
5

tôi thêm một MapFragment để ứng dụng của tôi và có mã sau đây, chuyển thể từ một bản đồ hướng dẫn:GooglePlayServicesUtil nút hộp thoại báo lỗi không có gì

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("Location Updates", "Google Play services is available."); 
     // Continue 
     return true; 

    // Google Play services was not available for some reason 
    } else { 
     GooglePlayServicesUtil.getErrorDialog(resultCode, this, 7).show(); 
     return false; 
    } 
} 

Tôi đang thử nghiệm trên một nhà máy thiết lập lại Galaxy tab 10.1 với lỗi thời dịch vụ google play. Vì vậy, khi tôi cố gắng để mở MapFragment tôi gọi servicesConnected() để kiểm tra và, như mong đợi, tôi nhận được một hộp thoại nói với tôi rằng tôi cần Dịch vụ của Google Play. Ở dưới cùng của hộp thoại, nó có một nút "Nhận các dịch vụ của Google Play" nhưng khi tôi nhấp vào nó, nó không có gì. LogCat của tôi xuất ra như sau:

07-23 15:30:43.580: W/GooglePlayServicesUtil(2515): Google Play services is missing. 
07-23 15:30:48.510: E/SettingsRedirect(2515): Can't redirect to app settings for Google Play services 

Tôi đã sau onConnectionFailed phương pháp (cơ bản là một copy-paste từ trang web nhà phát triển Android):

public void onConnectionFailed(ConnectionResult connectionResult) { 
    /* 
    * Google Play services can resolve some errors it detects. 
    * If the error has a resolution, try sending an Intent to 
    * start a Google Play services activity that can resolve 
    * error. 
    */ 
    if (connectionResult.hasResolution()) { 
     try { 
      // Start an Activity that tries to resolve the error 
      connectionResult.startResolutionForResult(
        this, 
        CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      /* 
      * Thrown if Google Play services canceled the original 
      * PendingIntent 
      */ 
     } catch (IntentSender.SendIntentException e) { 
      // Log the error 
      e.printStackTrace(); 
     } 
    } else { 
     /* 
     * If no resolution is available, display a dialog to the 
     * user with the error. 
     */ 
     GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 7).show(); 
    } 
} 

Tại sao không phải là làm việc này? Bất kỳ sự trợ giúp nào đều sẽ là tuyệt vời.

Here 'Trang nhà phát triển Android' tôi đang làm việc và here là một bài đăng SO liên quan đến nó.

Sửa

tôi nhận ra tôi không có Tài khoản Google được thiết lập trên thiết bị vì vậy tôi thiết lập một, nhưng nó làm cho không có sự khác biệt.

Trả lời

3

Mã này đang làm việc cho tôi:

boolean checkGooglePlayServicesAvailable() { 
    final int connectionStatusCode = GooglePlayServicesUtil 
     .isGooglePlayServicesAvailable(getActivity()); 
    Log.i(DEBUG_TAG, 
    "checkGooglePlayServicesAvailable, connectionStatusCode=" 
    + connectionStatusCode); 
    if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) { 
     showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode); 
     return false; 
    } 
    Toast t = Toast.makeText(getActivity(), 
     "Google Play service available", Toast.LENGTH_LONG); 
    t.show(); 
    return true; 
} 

void showGooglePlayServicesAvailabilityErrorDialog(
    final int connectionStatusCode) { 
    getActivity().runOnUiThread(new Runnable() { 
    public void run() { 
    final Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
     connectionStatusCode, getActivity(), 
     REQUEST_GOOGLE_PLAY_SERVICES); 
     if (dialog == null) { 
       Log.e(DEBUG_TAG, 
         "couldn't get GooglePlayServicesUtil.getErrorDialog"); 
       Toast.makeText(getActivity(), 
         "incompatible version of Google Play Services", 
         Toast.LENGTH_LONG).show(); 
      } 
      //this was wrong here -->dialog.show(); 
     } 
    }); 
} 
+0

Tôi đã thử điều này và nó đã hoạt động một lần nhưng đã lộn xộn. Vì vậy, tôi đã thay đổi một số mã khác, gỡ cài đặt dịch vụ google play và thử lại và nó không hoạt động. Tôi đã xóa tất cả các thay đổi của mình và nó vẫn không hoạt động ... –

+0

Bây giờ nó hoạt động trở lại. Không thay đổi một điều –

+0

Mã "không nên" chạy đến dòng đó, nhưng chỉ trong trường hợp: bạn gọi dialog.show() bên trong câu lệnh if (dialog == null) – Ripityom

0

tôi đi qua cùng một vấn đề. Vấn đề là hộp thoại phải được tạo ra trong chuỗi giao diện người dùng và hàm hiển thị() cũng phải được gọi trong chuỗi giao diện người dùng.

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