2015-11-20 74 views
0

我想註冊一個gcm令牌但得到異常。註冊gcm令牌時應用程序崩潰(ExceptionInInitializerError)

我的android設備的os版本是android 5.0。

這是我的代碼註冊gcm令牌。

public static String refreshRegistationId(final Context context) { 
    String token = ""; 
    try { 
     // [START register_for_gcm] 
     // Initially this call goes out to the network to retrieve the token, subsequent calls 
     // are local. 
     // [START get_token] 
     InstanceID instanceID = InstanceID.getInstance(context); 
     // R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json. 
     // See https://developers.google.com/cloud-messaging/android/start for details on this file. 
     token = instanceID.getToken(SENDER_ID, 
       GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
     // [END get_token] 
     Log.i(TAG, "GCM Registration Token: " + token); 
     NidusTool.putStringSharedPreference(context, GCM_REGISTER_TOKEN, token); 

     // TODO: Implement this method to send any registration to your app's servers. 
     // sendRegistrationToServer(token); 

     // Subscribe to topic channels 
     // subscribeTopics(token); 

     // [END register_for_gcm] 
    } catch (Exception e) { 
     Log.d(TAG, "Failed to complete token refresh", e); 
    } 
    // Notify UI that registration has completed, so the progress indicator can be hidden. 
    // Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE); 
    // LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); 
    return token; 

} 

ExceptionInInitializerError調用instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null)

時,堆棧跟蹤

java.lang.ExceptionInInitializerError 
    at java.lang.Class.classForName(Native Method) 
    at java.lang.Class.forName(Class.java:306) 
    at java.security.Provider$Service.newInstance(Provider.java:1072) 
    at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151) 
    at java.security.Signature.tryAlgorithmWithProvider(Signature.java:217) 
    at java.security.Signature.tryAlgorithm(Signature.java:203) 
    at java.security.Signature.getSignature(Signature.java:175) 
    at java.security.Signature.getInstance(Signature.java:105) 
    at com.google.android.gms.iid.zzc.zza(Unknown Source) 
    at com.google.android.gms.iid.zzc.zza(Unknown Source) 
    at com.google.android.gms.iid.zzc.zzb(Unknown Source) 
    at com.google.android.gms.iid.zzc.zza(Unknown Source) 
    at com.google.android.gms.iid.InstanceID.zzc(Unknown Source) 
    at com.google.android.gms.iid.InstanceID.getToken(Unknown Source) 

這是我的gradle中出現的依賴。

// for gcm to use support-annotations 23.0.1 
compile 'com.android.support:support-annotations:23.0.1' 
compile 'com.google.android.gms:play-services-gcm:8.1.0' 
compile 'com.android.support:appcompat-v7:23.0.0' 

我不明白爲什麼我會得到這樣的例外。

謝謝。

+0

你有檢查你的清單和使用Internet權限? –

+0

您可以針對Play Services 8.3編譯com.google.android.gms:play-services-gcm:8.3.0''進行編譯並查看是否已解決? – KayAnn

+0

嗨,這個問題隨機發生。有時它可以正常工作,但有時會失敗並拋出異常。 –

回答

0

變化從您buil.gradle的依賴:

compile 'com.google.android.gms:play-services-gcm:8.1.0'

到:
compile 'com.google.android.gms:play-services-gcm:8.3.0'

此外,檢查SDK管理器Android Studio中,以確保你已經下載了最新版本/修訂Google Play服務。

因此,一旦你改變依賴關係,同步你的項目,你應該全部設置。 More info。請注意,您可以通過在此社區或Google搜索中進行快速搜索來找到類似問題的答案,以備將來參考。

相關問題