2013-06-18 68 views
2

Im遵循這裏的教程http://developer.android.com/google/gcm/gs.html爲了設置GCM。我目前正在嘗試註冊設備的過程。 但是,由於某種原因,應用程序似乎總是崩潰在 gcm = GoogleCloudMessaging.getInstance(this);Android谷歌雲消息傳遞樣本不工作

stacktrace似乎指向以下內容: 06-18 13:42:20.909:I/dalvikvm(11613):找不到方法com.google.android.gms.gcm.GoogleCloudMessaging.getInstance,引用自方法pushNotification.PushNotification $ 1.run

下面有什麼,我有一個樣品至今

public PushNotification(Context c, Activity activity) 
{ 
    context = c; 
    this.activity = activity; 
    regid = getRegistrationId(context); 

    if (regid.length() == 0) { 
     Log.d("IN PUSHNOTIFICATION ","NOT REGISTERED. REGISTERING NOW....."); 
     registerBackground(); 
    } 
    Log.d("IN PUSHNOTIFICATION ","REGISTRATION COMPLETE....."); 
    Log.d("IN PUSHNOTIFICATION ","REGISTRATION ID IS: " + regid); 
    gcm = GoogleCloudMessaging.getInstance(activity); //never reaches this code 

} 

private void registerBackground() { 

    Thread thread = new Thread(new Runnable() 
    { 
     public void run() 
     { 
      String msg = ""; 
      try { 
       if (gcm == null) { 
        Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. gcm == NULL"); 
        gcm = GoogleCloudMessaging.getInstance(context); 
        Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. AFTER gcm.GETINSTANCE"); 
       } 
       Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. BEFORE gcm.register"); 
       regid = gcm.register(SENDER_ID); 
       msg = "Device registered, registration id=" + regid; 
       Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. regid == " + regid); 
       // You should send the registration ID to your server over HTTP, 
       // so it can use GCM/HTTP or CCS to send messages to your app. 

       // For this demo: we don't need to send it because the device 
       // will send upstream messages to a server that echo back the message 
       // using the 'from' address in the message. 

       // Save the regid - no need to register again. 
       setRegistrationId(context, regid); 
      } catch (IOException ex) { 
       Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. EXCEPTION ERROR ERROR: ex = " + ex.getMessage()); 
       msg = "Error :" + ex.getMessage(); 
      } 
     } 
    } 
    ); 
    thread.start(); 
} 

以下是完整的堆棧跟蹤

06-18 13:42:20.909:d/IN PUSHN指令(11613):未註冊。 NOW REGISTERING ..... 06-18 13:42:20.909:I/dalvikvm(11613):從方法pushNotification無法 find方法 com.google.android.gms.gcm.GoogleCloudMessaging.getInstance, 引用。 PushNotification $ 1.run 06-18 13:42:20.909:W/dalvikvm(11613):VFY:無法解析靜態方法 5967:Lcom/google/android/gms/gcm/GoogleCloudMessaging; .getInstance (Landroid/content /上下文;)LCOM /谷歌/機器人/克/ GCM/Google雲端通訊; 06-18 13:42:20.909:D/dalvikvm(11613):VFY:替換操作碼0x71 0x0015 06-18 13:42:20.909:I/dalvikvm(11613):找不到方法 com.google。 android.gms.gcm.GoogleCloudMessaging.register,從方法pushNotification.PushNotification引用 $ 1.run 06-18 13:42:20.909:W/dalvikvm(11613):VFY:無法解決虛擬方法 5969:LCOM /谷歌/android/gms/gcm/GoogleCloudMessaging;.register ([Ljava/lang/String;)Ljava/lang/String; 06-18 13:42:20.909: D/dalvikvm(11613):VFY:將操作碼0x6e替換爲0x0039 06-18 13:42:20.909:D/IN PUSHNOTIFICATION(11613):REGISTRATION COMPLETE ..... 06-18 13:42:20.909:D/INPHNICATION(11613):註冊ID是: 06-18 13:42:20.909:D/AndroidRuntime(11613):關閉VM 06-18 13:42:20.909 :W/dalvikvm(11613):threadid = 1:線程退出 未捕獲異常(group = 0x2b542210)06-18 13:42:20.909:D/IN PUSHNOTIFICATION(11613):背景。 gcm == NULL 06-18 13:42:20.909:W/dalvikvm(11613):threadid = 20:線程退出 未捕獲的異常(group = 0x2b542210)06-18 13:42:20.909: I/Process 11613):發送信號。 PID:11613 SIG:9 06-18 13:42:20.919:I/ActivityManager(278):進程com.gotoohlala(pid 11613)已經死亡。

+0

您是否[設置Google Play服務SDK](http://developer.android.com/google/play-services/setup.html)? – Eran

+0

我相信如此。我已將google-play-services_lib添加爲項目的參考。我還在項目的另一個包中使用GoogleMaps v2,它運行得很好。所以我很確定我有谷歌播放服務。 – hervens

回答

4

我解決了這個問題。 雖然我鏈接到google-play-services_lib項目,但我還必須將jar文件google-play-services_lib.jar添加到項目中。

+0

在我的情況下,我沒有在我的設備 –

+0

上安裝最新的android-play-services。 –

1

對於其他人可能會像我這樣的bug發瘋。 以下錯誤代表同樣的事情:

找不到方法com.google.android.gms.gcm.GoogleCloudMessaging.getInstance java.lang.NoClasDefFound:.....Google雲端通訊

爲Google雲端通訊類的JAR文件不出口到Android運行。

因爲默認情況下,Android的製造商沒有出口的jar文件弗羅姆Android Private Library

您可以雙擊通過查看bin/dexedLibs文件夾檢查,如果仍然不能看到google-play-services-4064834317375a1fffbbc48c3994c697.jar < - 該文件不會導出到運行時。

解決方案: Project Properties/Java Build Path/Order and Export/檢查Android Private Library 現在你所有的jar文件會一起去的Android設備,而不再出現運行錯誤。