运行Android项目 Unable to instantiate application, IllegalAccessException

  • Post author:
  • Post category:其他


运行Android项目报如下错误


06-08 14:08:52.370 12415-12415/com.dobest E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.dobest, PID: 12415

java.lang.RuntimeException: Unable to instantiate application com.dobest.common.AppContext: java.lang.IllegalAccessException: void com.dobest.common.AppContext.<init>() is not accessible from java.lang.Class<android.app.Instrumentation>

at android.app.LoadedApk.makeApplication(LoadedApk.java:806)

at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5407)

at android.app.ActivityThread.-wrap2(ActivityThread.java)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1568)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:160)

at android.app.ActivityThread.main(ActivityThread.java:6200)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)

Caused by: java.lang.IllegalAccessException: void com.dobest.common.AppContext.<init>() is not accessible from java.lang.Class<android.app.Instrumentation>

at java.lang.Class.newInstance(Native Method)

at android.app.Instrumentation.newApplication(Instrumentation.java:1008)

at android.app.Instrumentation.newApplication(Instrumentation.java:993)

at android.app.LoadedApk.makeApplication(LoadedApk.java:800)

at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5407)

at android.app.ActivityThread.-wrap2(ActivityThread.java)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1568)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:160)

at android.app.ActivityThread.main(ActivityThread.java:6200)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)

纠结了很长时间才发现原来是自定义的appcontext 中一个无参构造方法写成了protected

 protected AppContext() {}

将其改为public 就可以正常使用了。另外如果方法定位为private 也是不能正常运行。



版权声明:本文为Marvinhq原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。