執行Android專案 Unable to instantiate application, IllegalAccessException
執行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 也是不能正常執行。