“无法在未密封的实例上执行此操作” java.lang.IllegalStateException异常 [英] "Cannot perform this action on a not sealed instance" java.lang.IllegalStateException exception

查看:1568
本文介绍了“无法在未密封的实例上执行此操作” java.lang.IllegalStateException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用android AccessibilityService 能够粘贴到其他应用 EditText 字段,但使用浏览器 testfields (模拟器默认浏览器或三星deault浏览器)它无法正常工作,抛出错误:

With android AccessibilityService able to paste in other app EditText Field, but with browser testfields (Emulator Default Browser or Samsung deault Browser) its not working, throwing error:


无法执行此操作在一个没有密封的实例上。

Cannot perform this action on a not sealed instance.

在android chrome浏览器中,有一些singnup文本字段可以工作但不适用于所有文本字段。

In android chrome browser with some singnup textfield its working but not for all textfields.

 @Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    AccessibilityNodeInfo source = event.getSource();
    if (source != null && ( event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED ) ) {
            // || event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED ) &&
            //event.getClassName().equals("android.widget.EditText")
            //) {
        ctx = getApplicationContext();
        ClipboardManager clipboard = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", "XYZ");
        clipboard.setPrimaryClip(clip);
        source.performAction(AccessibilityNodeInfo.ACTION_PASTE); 
        //Not Working, always return false.

        //Tried with other options
        Bundle argumentsTest = new Bundle();
        argumentsTest.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "Bundle Test Data");
        source.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT,argumentsTest )
        // Not Working, throw java.lang.IllegalStateException exception
        //Message: "Cannot perform this action on a not sealed instance"
    }
}   


推荐答案

我不喜欢相信你正试图做你认为你想做的事情。

I don't beleive you're trying to do what you think you're trying to do.

当您设置accessibilityNodeInfo的text时,您正在查找的是该对象的text属性,因为它与您的辅助功能服务有关。这并不意味着您正在更改accessibilityNodeInfo对象引用的EditText框的文本。当您的辅助功能服务获取此对象时,这两个对象彼此完全分离。即使您的代码成功运行,您也无法获得预期的结果。现在,至于为什么你不能执行这个动作,知道这一点应该是显而易见的。对于能够修改其拥有的节点的辅助功能服务,实际上没有意义。因此它们变得密封(将其视为常量的运行时执行)。可访问性节点在其生命周期的各个点处变得密封和未密封。可以访问未密封节点信息的框架部分是View类和私有API。任何与辅助功能服务相关的任务都将处理密封的只读实例。

When you set the "text" of an accessibilityNodeInfo, what you're chaning is the text property of that object, as it pertains to your accessibility service. THIS DOES NOT mean, that you are changing the text of the EditText box, that the accessibilityNodeInfo object references. By the time your accessibility service gets this object, the two objects are quite separate from each other. Even if your code were to run successfully, you would not be getting the results you are expecting. Now, as for why you cannot perform this action, knowing this it should be obvious. For an accessibility service to be able to modify the nodes it has, doesn't really make sense. So they become sealed (think of this as a run time enforcement of a constant). Accessibility nodes become sealed and unsealed at various points in their lifetime. The parts of the framework that have access to unsealed node infos are View classes and private APIs. Any accessibility service related tasks are going to be dealing with sealed, read-only instances.

至于为什么原始解决方案不起作用,我相信我们没有足够的信息。 ACTION_PASTE方法(近似)是正确的方法,但是,在使用Web浏览器时,存在大量问题。浏览器版本,Android版本,设备版本,网站等都发挥作用。特别是如果您的设置足够大,不能使用新的WebView(纯铬webview,而不是基于过时版本的WebKit的奇怪嵌入式移动WebView的旧4. +方法,现在永远不会更新)。我建议您使用最新的Nexus设备测试您的代码,至少使用Android 5.0并查看您的代码是否有效。如果您无法执行此操作,请报告您的设置的版本信息。如果你已经在哪个网站上?

As for why your original solution is not working, I believe that we do not have enough information. The "ACTION_PASTE" approach is (approximately) the correct approach, HOWEVER, there are an abundance of issues when doing so with web browsers. Browser version, Android version, device version, website, etc all play a role. Especially if your set up is old enough to not use the new WebView (pure chromium webview, rather than the old 4.+ approach of odd embedded mobile WebViews based on outdated versions of WebKit, which will now never be updated). I recommend testing your code on an up to date Nexus device, using at least Android 5.0 and seeing if your code works. If you cannot do this, report version information for your set up. If you already are, on what website?

这篇关于“无法在未密封的实例上执行此操作” java.lang.IllegalStateException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆