GCM无法实例化receiver / ClassNotFoundException

时间:2014-05-17 11:32:42

标签: android google-cloud-messaging

以下是我通过GCM向设备发送通知时收到的信息。我到处搜索,我仍然无法弄清楚我做错了什么。

05-17 13:02:41.817: E/AndroidRuntime(2488): FATAL EXCEPTION: main
05-17 13:02:41.817: E/AndroidRuntime(2488): java.lang.RuntimeException: Unable to instantiate receiver com.amo.android.gcm.NotificationsReceiver: java.lang.ClassNotFoundException: Didn't find class "com.amo.android.gcm.NotificationsReceiver" on path: /data/app/com.amo.android-1.apk
05-17 13:02:41.817: E/AndroidRuntime(2488):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2357)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at android.app.ActivityThread.access$1500(ActivityThread.java:141)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at android.os.Looper.loop(Looper.java:137)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at java.lang.reflect.Method.invoke(Method.java:511)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at dalvik.system.NativeStart.main(Native Method)
05-17 13:02:41.817: E/AndroidRuntime(2488): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.amo.android.gcm.NotificationsReceiver" on path: /data/app/com.amo.android-1.apk
05-17 13:02:41.817: E/AndroidRuntime(2488):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-17 13:02:41.817: E/AndroidRuntime(2488):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2352)
05-17 13:02:41.817: E/AndroidRuntime(2488):     ... 10 more

这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.amo.android"
    android:versionCode="2"
    android:versionName="1.2" >

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- GCM -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission android:name="com.amo.android.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.amo.android.permission.C2D_MESSAGE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.amo.android.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.amo.android.gcm.NotificationsReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.amo.android" />
            </intent-filter>
        </receiver>
        <service android:name="com.amo.android.gcm.GcmMessageHandler" />

        <activity android:name="com.amo.android.MainActivity"       android:screenOrientation="portrait" />
        <activity android:name="com.amo.android.SuggestActivity"    android:screenOrientation="portrait" />
        <activity android:name="com.amo.android.EventActivity"      android:screenOrientation="portrait" />
        <activity android:name="com.amo.android.CommentsActivity"   android:screenOrientation="portrait" />
        <activity android:name="com.amo.android.SettingsActivity"   android:screenOrientation="portrait" />
        <activity android:name="com.amo.android.FeedbackActivity"   android:screenOrientation="portrait" />
        <activity android:name="com.amo.android.EventsListActivity" android:screenOrientation="portrait" />
        <activity android:name="com.amo.android.LikersActivity"     android:screenOrientation="portrait" />
        <activity android:name="com.amo.android.gcm.NotificationsDebugActivity"     android:screenOrientation="portrait" />

        <!-- Facebook -->
        <meta-data android:value="@string/facebook_app_id" android:name="com.facebook.sdk.ApplicationId"/> 

        <activity android:name="com.facebook.LoginActivity" />

    </application>

</manifest>

这是我的接收者:

package com.amo.android.gcm;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public class NotificationsReceiver extends WakefulBroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.i("GcmBroadcastReceiver", "receive message !");

        ComponentName comp = new ComponentName(context.getPackageName(), GcmMessageHandler.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

请帮忙。

1 个答案:

答案 0 :(得分:1)

我通过从头开始重新创建项目来找到解决方案。我还是不知道出了什么问题。

问题解决了!