为什么android不能看到这个内部类?

时间:2014-04-11 13:29:59

标签: android google-cloud-messaging

我的GcmBroadcastHandler内有一个Service,就像这样:

public class SyncService extends Service {

// lots of other stuff in this class

    public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "GCM received!");
            downloadChanges();
        }
    }
}

SyncService位于名为service的包中。 我已经在AndroidManifest.xml中声明了服务和接收器,如此:

<application
android:name="com.appnl.myapp.CustomApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" >

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

<!-- android.name=".GcmBroadcastReceiver" -->

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

<service
    android:name="service.SyncService"
    android:enabled="true"
    android:exported="false"
    android:icon="@drawable/ic_launcher"
    android:label="@string/service_name" >
    <intent-filter>
        <action android:name="service.SyncService" />
    </intent-filter>
</service>

<!-- activity list -->

然而,当android处理传入的GCM消息时,我想以下ClassNotFoundException

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

有关为何无法在GcmBroadcastHandler内找到Service的任何想法?是因为我在清单中正确宣布了它吗?

1 个答案:

答案 0 :(得分:2)

使用$声明清单中的内部类,使用其外部类,如.. chnage这个

android:name=".GcmBroadcastReceiver"

进入

android:name="packageName.SyncService$GcmBroadcastReceiver"

有关详情,请查看此Registering Inner class in manifest