1

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 ?

            

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am having the same issue, but when running on a device from my Android Studio debugger, it works. When running from another (managed) device I get this problem. I understand the "don't do constructor" thing and I will remove it, but why would it work from my debugger?
@BartFriederichs: If you have Instant Run enabled, it is possible that something about how that is set up avoids this problem. Otherwise, I have no idea, sorry.
I disabled Instant Run and it crashed on me. So it looks like something Instant Run does indeed.
Also note that is does work if the constructor is public.
There are plenty of reasons why you might want t have code in an activity constructor. The issue is that the constructor for your class is not public. Or that instant run has lost its mind (which it often does). If that's the case, do a clean build and the problem will go away.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.