Unable to instantiate receiver, java.lang.IllegalAccessException: access to class not allowed

青春壹個敷衍的年華 提交于 2020-06-08 17:28:30

问题


In the manifest I've declared the receiver element correctly to the best of my knowledge. But the Receiver never gets called when I send out the broadcast. The log cat shows.

07-22 23:51:49.181: E/AndroidRuntime(3799): FATAL EXCEPTION: main
07-22 23:51:49.181: E/AndroidRuntime(3799): java.lang.RuntimeException: Unable to instantiate receiver com.example.orderedbroadcastreceiver.HigherPriorityReceiver: java.lang.IllegalAccessException: access to class not allowed
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2333)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread.access$1500(ActivityThread.java:139)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.os.Looper.loop(Looper.java:137)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread.main(ActivityThread.java:4899)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at java.lang.reflect.Method.invoke(Method.java:511)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at dalvik.system.NativeStart.main(Native Method)
07-22 23:51:49.181: E/AndroidRuntime(3799): Caused by: java.lang.IllegalAccessException: access to class not allowed
07-22 23:51:49.181: E/AndroidRuntime(3799):     at java.lang.Class.newInstanceImpl(Native Method)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at java.lang.Class.newInstance(Class.java:1319)
07-22 23:51:49.181: E/AndroidRuntime(3799):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2328)
07-22 23:51:49.181: E/AndroidRuntime(3799):     ... 10 more

Code

class HigherPriorityReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "HigherPriorityReceiver", 0).show();
    setResultCode(1985);
    setResultData("DOB");
    //abortBroadcast();
}

回答1:


make class HigherPriorityReceiver a public class




回答2:


In Kotlin it must be a class, not an object.




回答3:


Answer

This class must be a public class

Code

public class HigherPriorityReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "HigherPriorityReceiver", 0).show();
        setResultCode(1985);
        setResultData("DOB");
        //abortBroadcast();
    }
}


来源:https://stackoverflow.com/questions/17794913/unable-to-instantiate-receiver-java-lang-illegalaccessexception-access-to-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!