Android的广播接收器错误:找不到类异常找不到、接收器、异常、错误

2023-09-05 07:48:46 作者:书煮日月

我有一个广播接收器设置,使一个弹出消息是我的应用程序的每个升级后显示给用户,或者如果这是第一次安装包。我测试了这对我的Droid运行Android 2.2同时作为一个全新的安装和运行1.5和1.6升级我的应用程序,以及在仿真器后,我看到的一切运行正常。

I have a Broadcast receiver setup so that a pop-up message is displayed to the user after each upgrade of my app, or if this is the first time the package is installed. I tested this on my Droid running Android 2.2 both as a fresh install and after upgrading my app, as well in the Emulator running 1.5 and 1.6, and I see everything run fine.

不过,我收到错误报告从用户,其中列出了以下异常:

However, I received an error report from a user that lists the following exception:

java.lang.RuntimeException: Unable to instantiate receiver 
com.name.pkg.FirstRunBroadcastReceiver: java.lang.ClassNotFoundException: com.name.pkg.app_name.FirstRunBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.name.pkg.app_name.apk]
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
at android.app.ActivityThread.access$3200(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.name.pkg.app_name.FirstRunBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.name.pkg.app_name.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780)
... 10 more

谁能告诉我为什么我的用户收到此异常?

Can someone tell me why one of my users received this exception?

在我的清单文件,我有事情的设置是这样的:

In my Manifest file, I have things setup like this:

<receiver android:name=".FirstRunBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACE"/>
        <data android:scheme="package" android:path="com.name.pkg.app_name">
    </intent-filter>
</receiver>

类FirstRunBroadcastReceiver是设置这样的:

The class FirstRunBroadcastReceiver is setup like this:

package com.name.pkg.app_name;

public class FirstRunBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Contenxt context, Intent intent)
    {
        Uri uri = intent.getData();
        if( uri.toString().compareTo("package:com.name.pkg.app_name") == 0 )
        {
            //set shared prefs data to determine if start-up message should be shown
        }
    }
}

当第一次安装,用于共享preFS默认值设置,以便将显示在弹出消息,但我不认为这有什么与此异常。

Upon a first time install, the default value for the shared prefs is set so that the pop-up message will be displayed, but I wouldn't think that would have anything to do with this exception.

我再次测试它,我没有收到异常。感谢您的帮助,您可以提供。

I tested it again, and I don't receive an exception. Thanks for any help you can provide.

推荐答案

从Android文档中的机器人:出口属性接收器:

from the android documentation on "android:exported" attribute for a receiver:

不管是不是广播接收器可以在其应用程序的外部来源收到的消息 - 真如果可以,和假,如果没有。如果假,广播接收机可接收的唯一消息是那些由具有相同的用户ID相同的应用程序或应用程序的组成部分发送。 默认值取决于广播接收器是否包含意图过滤器。不存在任何过滤器意味着它只能由指定其确切类名意图对象上调用。这意味着,接收器仅用于应用内部使用(因为更为通常不会知道类名称)。因此,在这种情况下,默认值是假。另一方面,至少一个过滤器的presence意味着广播接收机旨在接收意图由系统或其它应用程序播放,那么该默认值是真

Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID. The default value depends on whether the broadcast receiver contains intent filters. The absence of any filters means that it can be invoked only by Intent objects that specify its exact class name. This implies that the receiver is intended only for application-internal use (since others would not normally know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the broadcast receiver is intended to receive intents broadcast by the system or other applications, so the default value is "true"

由于您的接收器有孩子的Intent为默认值的android:出口是真实的。只是说明这一明确的,它应该发挥的罚款。

Since your receiver has child intents the default value for android:exported is true. Just state this explicitly and it should function fine.

<receiver android:name=".FirstRunBroadcastReceiver" android:exported="true">
<intent-filter>
    <action android:name="android.intent.action.PACKAGE_REPLACE"/>
    <data android:scheme="package" android:path="com.name.pkg.app_name">
</intent-filter>