运行Android项目,报错java.lang.IllegalAccessException: access to constructor not allowe 博客分类: android access to constructor not alloweapplication
程序员文章站
2024-02-16 08:08:58
...
修改了AndroidManifest.xml,设置<application>的属性android:name="自定义的Aplication",结果运行Android项目,报下面错误:
08-18 02:27:41.660: D/dalvikvm(649): newInstance failed: Lcom/foxhis/untils/ActivityApplication;.<init>() not accessible to Landroid/app/Instrumentation; 08-18 02:27:41.660: D/AndroidRuntime(649): Shutting down VM 08-18 02:27:41.660: W/dalvikvm(649): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 08-18 02:27:41.680: E/AndroidRuntime(649): FATAL EXCEPTION: main 08-18 02:27:41.680: E/AndroidRuntime(649): java.lang.RuntimeException: Unable to instantiate application com.foxhis.untils.ActivityApplication: java.lang.IllegalAccessException: access to constructor not allowed 08-18 02:27:41.680: E/AndroidRuntime(649): at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:649) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4232) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.app.ActivityThread.access$3000(ActivityThread.java:125) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.os.Handler.dispatchMessage(Handler.java:99) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.os.Looper.loop(Looper.java:123) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-18 02:27:41.680: E/AndroidRuntime(649): at java.lang.reflect.Method.invokeNative(Native Method) 08-18 02:27:41.680: E/AndroidRuntime(649): at java.lang.reflect.Method.invoke(Method.java:521) 08-18 02:27:41.680: E/AndroidRuntime(649): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-18 02:27:41.680: E/AndroidRuntime(649): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-18 02:27:41.680: E/AndroidRuntime(649): at dalvik.system.NativeStart.main(Native Method) 08-18 02:27:41.680: E/AndroidRuntime(649): Caused by: java.lang.IllegalAccessException: access to constructor not allowed 08-18 02:27:41.680: E/AndroidRuntime(649): at java.lang.Class.newInstanceImpl(Native Method) 08-18 02:27:41.680: E/AndroidRuntime(649): at java.lang.Class.newInstance(Class.java:1429) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.app.Instrumentation.newApplication(Instrumentation.java:957) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.app.Instrumentation.newApplication(Instrumentation.java:942) 08-18 02:27:41.680: E/AndroidRuntime(649): at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:644) 08-18 02:27:41.680: E/AndroidRuntime(649): ... 11 more
错误提示是,构造方法的权限不对,招拒。
检查了整个Application,才发现,原来有一个无参数的构造方法,被设计成private。
修改其为public即可。
其他:
对于自定义的Application,如果里面定义的参数是,只仅仅该类中使用,则可以定义为private,私有属性。 如果是其他地方也使用,必须定义为public。 类中方法,全部定义为public。