Android GCM无法实例化接收器

时间:2014-04-15 18:17:53

标签: java android google-play google-play-services google-cloud-messaging

我正在尝试为应用实现Android推送通知。我按照这里的步骤进行了操作:link

然而,当我尝试在我的设备上运行应用程序时,我收到错误,即我的apk中找不到GcmBroadcastReceiver ...虽然它在那里。

我的工作区中有google-play-services-lib作为库。我把我的项目链接到这个库。

以下是崩溃发生时我遇到的错误。

04-15 20:59:28.722: E/AndroidRuntime(10970): FATAL EXCEPTION: main
04-15 20:59:28.722: E/AndroidRuntime(10970): java.lang.RuntimeException: Unable to instantiate receiver sg.ignitedigital.AllAccess.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "sg.ignitedigital.AllAccess.GcmBroadcastReceiver" on path: /data/app/sg.ignitedigital.AllAccess-1.apk
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2493)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread.access$1600(ActivityThread.java:159)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1392)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.os.Looper.loop(Looper.java:137)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at java.lang.reflect.Method.invoke(Method.java:525)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at dalvik.system.NativeStart.main(Native Method)
04-15 20:59:28.722: E/AndroidRuntime(10970): Caused by: java.lang.ClassNotFoundException: Didn't find class "sg.ignitedigital.AllAccess.GcmBroadcastReceiver" on path: /data/app/sg.ignitedigital.AllAccess-1.apk
04-15 20:59:28.722: E/AndroidRuntime(10970):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2488)
04-15 20:59:28.722: E/AndroidRuntime(10970):    ... 10 more

以下是我在清单文件中添加服务和接收器的方法:

    <permission android:name="sg.ignitedigital.AllAccess.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="sg.ignitedigital.AllAccess.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <receiver android:name=".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="sg.ignitedigital.AllAccess" />
        </intent-filter>
    </receiver>        

    <service android:name="sg.ignitedigital.AllAccess.GcmIntentService" />

我已经尝试过这么多次。为什么它没有找到我的GcmBroadcastReceiver文件,如果它在包中...  

修正:

最终我发现了这个问题。就我而言,我的项目使用了一个库项目,它有一个支持v4 jar的版本,我的项目有一个不同版本的支持v4 jar。因为那些不匹配,我收到了这个错误。一旦我同步了相同版本的支持库,一切运行良好。

1 个答案:

答案 0 :(得分:2)

整个上午我也遇到了同样的问题。我查看了StackOverflow和Google本身。

问题是当您扩展 WakefulBroadcastReceiver 以创建 GcmBroadcastReceiver 时,必须导出v4支持库以初始化该类。否则,在我的情况下,您将在运行时遇到 ClassNotFoundException 问题,因为支持库位于 libs 文件夹中,而不是完全在Java Build Path中导出。

So, the thorough steps are <br />
1. get the proper android-support-v4-libs from your android base folder (android-sdk-XXX/extras/android/support/v4/android-support-v4.jar). <br />
2. put under **libs** folder of your main project. (override other android-support-v4-libs.jar if you have other project referencing from your main project). <br />
3. Right Click on android-support-v4-libs.jar > Build > Add to Build Path.

检查AndroidManifest.xml的其他要点是

<receiver
 android:name=".receiver.GcmBroadcastReceiver"
 android:permission="com.google.android.c2dm.permission.SEND" 
 android:exported="true"
 android:enabled="true"
 >
 <intent-filter>
      <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
      <category android:name="yourApplicationPackageName" />
 </intent-filter>
</receiver>

<service
    android:name=".GcmIntentService"
    />