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)已經死亡。
您是否[設置Google Play服務SDK](http://developer.android.com/google/play-services/setup.html)? – Eran
我相信如此。我已將google-play-services_lib添加爲項目的參考。我還在項目的另一個包中使用GoogleMaps v2,它運行得很好。所以我很確定我有谷歌播放服務。 – hervens