简体   繁体   中英

java.lang.RuntimeException: Unable to instantiate activity

how can I fix bellow errors from my logcat

   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hesh.ballc/hesh.ballc.MainActivity}: java.lang.IllegalAccessException: hesh.ballc.MainActivity() is not accessible from class android.app.Instrumentation
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)    



Caused by: java.lang.IllegalAccessException: hesh.ballc.MainActivity() is not accessible from class android.app.Instrumentation
        at java.lang.Class.newInstance(Class.java:1603)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)

There is no errors in code as I can see, after running my program it stops in my emulator and I get bellow errors, should I import some libary ?

According to the stack trace, you implemented a constructor on hesh.ballc.MainActivity .

DO NOT DO THIS . There are few, if any, scenarios in which having a constructor on an Activity subclass is the appropriate thing to do.

Whatever code you have in that constructor should go into the activity's onCreate() method, probably after the super.onCreate() call.

There was another case of this occurring not covered that I thought I would provide details for. When you have library dependencies and import modules, be sure that the other libraries do not have a manifest file that lists the module as a launchable component.

This might be an oddity to have this in a library, but if it finds itself in there (from instance, following a default start of a project) then you simply need to remove the offending module's manifest launcher component and reimport the module.

You will see a similar stacktrace exception as indicated above in the post, but it will reference a class that is not the main project (a library module project trying to be launched). It seems to merge some manifest files and if setup improperly you may get this behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM