2013-06-18 67 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设备,而不再出现运行错误。