我从https://code.google.com/p/gcm/
导入了该项目它的构建,当我运行项目时,有时我的应用程序崩溃并给我一个错误,如
12-03 19:26:20.416: E/AndroidRuntime(17274): java.lang.RuntimeException: Unable to instantiate receiver com.google.android.gcm.demo.app.GcmBroadcastReceiver: java.lang.ClassNotFoundException: com.google.android.gcm.demo.app.GcmBroadcastReceiver
12-03 19:26:20.416: E/AndroidRuntime(17274): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2222)
12-03 19:26:20.416: E/AndroidRuntime(17274): at android.app.ActivityThread.access$1500(ActivityThread.java:138)
12-03 19:26:20.416: E/AndroidRuntime(17274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
12-03 19:26:20.416: E/AndroidRuntime(17274): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 19:26:20.416: E/AndroidRuntime(17274): at android.os.Looper.loop(Looper.java:213)
12-03 19:26:20.416: E/AndroidRuntime(17274): at android.app.ActivityThread.main(ActivityThread.java:4787)
12-03 19:26:20.416: E/AndroidRuntime(17274): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 19:26:20.416: E/AndroidRuntime(17274): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 19:26:20.416: E/AndroidRuntime(17274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-03 19:26:20.416: E/AndroidRuntime(17274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
12-03 19:26:20.416: E/AndroidRuntime(17274): at dalvik.system.NativeStart.main(Native Method)
12-03 19:26:20.416: E/AndroidRuntime(17274): Caused by: java.lang.ClassNotFoundException: com.google.android.gcm.demo.app.GcmBroadcastReceiver
12-03 19:26:20.416: E/AndroidRuntime(17274): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-03 19:26:20.416: E/AndroidRuntime(17274): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-03 19:26:20.416: E/AndroidRuntime(17274): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-03 19:26:20.416: E/AndroidRuntime(17274): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2217)
12-03 19:26:20.416: E/AndroidRuntime(17274): ... 10 more
12-03 19:26:22.496: I/Process(17274): Sending signal. PID: 17274 SIG: 9
但GcmBroadcastReceiver在我的包中
并且在清单声明中也正确
Click事件的源代码是
public void onClick(final View view) {
if (view == findViewById(R.id.send)) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
Bundle data = new Bundle();
data.putString("my_message", "Hello World");
data.putString("my_action", "com.google.android.gcm.demo.app.ECHO_NOW");
String id = Integer.toString(msgId.incrementAndGet());
System.out.println("GCM Adhyakhs = "+SENDER_ID + "@gcm.googleapis.com");
gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
msg = "Sent message";
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);