gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:10:31 26 4
gpt4 key购买 nike

我的 Service 中有一个 GcmBroadcastHandler,如下所示:

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

关于为什么它在我的 Service 中找不到 GcmBroadcastHandler 有什么想法吗?是不是因为我在manifest里声明正确了?

最佳答案

使用 $ 声明 list 中的内部类,使用其外部类,例如...改变这个

android:name=".GcmBroadcastReceiver"

进入

android:name="packageName.SyncService$GcmBroadcastReceiver"

更多信息请查看 Registering Inner class in manifest

关于android - 为什么android看不到这个内部类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23014040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com