【问题标题】:Android GCM Client exceptionAndroid GCM 客户端异常
【发布时间】:2013-09-06 04:35:01
【问题描述】:

我最近开始了解 Android GCM。我正在尝试在here 的 Android 开发人员中提供的 GCM 客户端示例,当我尝试单击 MainActivity 中的发送按钮时,它给了我一些异常,如下所示:

09-06 00:16:41.480: E/AndroidRuntime(1246): FATAL EXCEPTION: main
09-06 00:16:41.480: E/AndroidRuntime(1246): java.lang.RuntimeException: Unable to instantiate receiver com.google.android.gcm.demo.app.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gcm.demo.app.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.google.android.gcm.demo.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.android.gcm.demo.app-1, /vendor/lib, /system/lib]]
09-06 00:16:41.480: E/AndroidRuntime(1246):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2405)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at android.app.ActivityThread.access$1500(ActivityThread.java:141)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at android.os.Looper.loop(Looper.java:137)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at java.lang.reflect.Method.invoke(Method.java:525)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at dalvik.system.NativeStart.main(Native Method)
09-06 00:16:41.480: E/AndroidRuntime(1246): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gcm.demo.app.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.google.android.gcm.demo.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.android.gcm.demo.app-1, /vendor/lib, /system/lib]]
09-06 00:16:41.480: E/AndroidRuntime(1246):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-06 00:16:41.480: E/AndroidRuntime(1246):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
09-06 00:16:41.480: E/AndroidRuntime(1246):     ... 10 more

(对不起,由于我的声誉不足,我无法发布图片。)

我正在使用模拟器如下

目标:Google API 级别 - 18

并向其中添加了 Google 帐户(我认为这不是必需的,因为我使用的是 android 4.3)。

在服务器端,我尝试构建 WCF 服务并发送 HTTP POST 请求,得到以下响应:

"状态:200 - 正常 id=0:1378440485753994%921c249af9fd7ecd"

此外,我还尝试使用helmibaraja.com 的 GCM 测试服务器,并得到以下响应

"{"multicast_id":6425765079749845115,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1378441452317757%921c249af9fd7ecd"} ]}"

所以我认为服务器端不是问题。

我希望有人在客户端、模拟器设置等方面纠正我...

顺便说一句,我还将“google-play-services_lib”导入到我的包资源管理器中,并添加了“gcm.jar”和“gcm-src. jar" 到我项目的 Java 构建路径。

提前致谢!

更正:上述异常是在演示客户端应用中单击按钮时引起的。

从服务器发送 GCM 时显示以下日志。

09-06 01:13:51.580: W/BroadcastQueue(274): Unable to launch app com.google.android.gcm.demo.app/10057 for broadcast Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=com.google.android.gcm.demo.app (has extras) }: process is bad
09-06 01:13:51.610: W/GTalkService(466): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.google.android.gcm.demo.app (has extras) }

【问题讨论】:

  • 请将您的清单和广播接收器类添加到问题中。

标签: android push-notification google-cloud-messaging


【解决方案1】:

框架无法找到广播接收器。您需要在清单文件中声明。

在接收者标签内:

<receiver
        android:name="com.google.android.gcm.demo.app.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="PACKAGE_NAME" />
        </intent-filter>
    </receiver>

【讨论】:

  • 异常仍然存在...我想知道以下是什么意思“在路径上:DexPathList[[zip file”/data/app/com.google.android.gcm.demo.app -1.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.android.gcm.demo.app-1, /vendor/lib, /system/lib]"
【解决方案2】:

试试这个:

转到 Project/Properties/Java Build Path/Order and Export -- 如果您使用它,请确保在 Android Dependencies 和支持库前面有一个检查。标记所有复选框。单击 Apply 并清理项目。

像这样添加:

<receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.test.android" />
        </intent-filter>
    </receiver>

应该是:com.google.android.gcm.GCMBroadcastReceiver 而不是.GCMBroadcastReceiver

这对我有用。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多