java.lang.IllegalStateException: Cannot perform this action on a not sealed instance
Asked Answered
D

2

8

I am using accessibility service to find a button on screen and click it, but for some reason I am getting the follow error:

java.lang.IllegalStateException: Cannot perform this action on a not sealed instance.
    at android.view.accessibility.AccessibilityNodeInfo.enforceSealed(AccessibilityNodeInfo.java:3046)
    at android.view.accessibility.AccessibilityNodeInfo.findAccessibilityNodeInfosByText(AccessibilityNodeInfo.java:1529)
    at com.myapp.adapters.Adapter_Click$11.run(Adapter_Click.java:1874)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Method.java:-2)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

And the line that it refers to is:

List<AccessibilityNodeInfo> list = source.findAccessibilityNodeInfosByText("Accept".toLowerCase());

And then I use this to click the button:

if (view.performAction(AccessibilityNodeInfo.ACTION_CLICK)) 
{
  Log.e(TAG, "Button clicked");
}

I didn't see any issues with this for the last year until I just recently updated to AndroidX. Is there a way to check if it is a sealed instance before trying to click it? I even tried to wrap it in a try/catch and it still crashes my app.

Debonair answered 23/2, 2019 at 16:25 Comment(1)
Can you share your .xml file which you have created to use Accessibility Service?Arjan
D
2

(It's kind of a pain, but if a node gets ditched most functionality on it breaks horribly -- even refresh(). It's strange, since as far as I know you're allowed to retain instances as long as you need them.)

You can easily check if one is still valid by checking if getClassName() returns null -- if it does, drop it like a hot potato! You can't do anything with it anymore -- don't even recycle() it because something already did that underneath your feet.

Dolhenty answered 16/9, 2019 at 20:29 Comment(4)
Can you please add to your answer, what might cause that object to be "ditched" right before the use of it?Hephaestus
The OS' accessibility service likes to recycle nodes on a regular basis. I had done a bunch of research on it before but I don't remember the specifics offhand. Btw, by ditch I meant recycle.Dolhenty
Hey where did you come up with the getClassName check? That's brilliant if it works... I'm just a bit hesitant to rely on it.Kraska
I think it was trial and error initially, then I checked the Android source code to check how it handles the recycling because it felt so obtuse. So, it should be reliable; I had no trouble with it, though that project was put aside after a few months so I can't claim we tested a lot of devices.Dolhenty
B
0

Sealing is done to make nodes immutable. These kind of exceptions often come when a service uses a node after it's been recycled.

Bucephalus answered 25/2, 2019 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.