无法从LogCat内部服务中解析字符串

时间:2017-10-03 13:36:57

标签: java android string

所以我从下面的函数中得到了一个这样的字符串(从LogCat中获取):

private OriginalPermissions identifyPermission(AccessibilityEvent event) {
    List < AccessibilityNodeInfo > mFoundNodeInfos;
    AccessibilityNodeInfo nodeInfo = event.getSource();
    for (Map.Entry < OriginalPermissions, String > permissions: pePers.entrySet()) {
        mFoundNodeInfos = nodeInfo.findAccessibilityNodeInfosByText(permissions.getValue());

        //Get Application Information
        Log.d("Information", appNamer(String.valueOf(mFoundNodeInfos)));

        if (mFoundNodeInfos.size() == 1) return permissions.getKey();
    }
    return OriginalPermissions.OTHER;
}

现在,这是返回的字符串:

[android.view.accessibility.AccessibilityNodeInfo@800290aa; boundsInParent: Rect(0, 0 - 566, 195); boundsInScreen: Rect(310, 691 - 876, 886); packageName: com.google.android.packageinstaller; className: android.widget.TextView; text: Allow Camera to take pictures and record video?; error: null; maxTextLength: -1; contentDescription: null; viewIdResName: com.android.packageinstaller:id/permission_message; checkable: false; checked: false; focusable: false; focused: false; selected: false; clickable: false; longClickable: false; contextClickable: false; enabled: true; password: false; scrollable: false; actions: [AccessibilityAction: ACTION_SELECT - null, AccessibilityAction: ACTION_CLEAR_SELECTION - null, AccessibilityAction: ACTION_ACCESSIBILITY_FOCUS - null, AccessibilityAction: ACTION_NEXT_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY - null, AccessibilityAction: ACTION_SET_SELECTION - null, AccessibilityAction: ACTION_SHOW_ON_SCREEN - null]]

现在我必须在两个单词Allowto之间输入字符串。我试过的是:

private String appNamer(String parse){
     parse = parse.substring(parse.indexOf("Allow") + 6 , parse.length());
     parse = parse.substring(0, parse.indexOf(' '));
     return parse;
}

此代码在个人基础上完美运行,但在我的Android应用程序中运行时,如下所示,我立即获得ArrayIndexOutOfBounds Exception

Log.d("Information", appNamer(appNamer(String.valueOf(mFoundNodeInfos))));

这是一个例外:

FATAL EXCEPTION: Thread-5
Process: com.triple.m.crabzilla.pepers, PID: 31783
java.lang.StringIndexOutOfBoundsException: length=2; regionStart=5; regionLength=-3
   at java.lang.String.substring(String.java:1931)
   at com.triple.m.crabzilla.pepers.PePersDetectorService.appNamer(PePersDetectorService.java:253)
   at com.triple.m.crabzilla.pepers.PePersDetectorService.identifyPermission(PePersDetectorService.java:243)
   at com.triple.m.crabzilla.pepers.PePersDetectorService.handlePermissionRequest(PePersDetectorService.java:80)
   at com.triple.m.crabzilla.pepers.PePersDetectorService.access$300(PePersDetectorService.java:29)
   at com.triple.m.crabzilla.pepers.PePersDetectorService$AccessibilityEventProcessor$1.run(PePersDetectorService.java:134)
   at java.lang.Thread.run(Thread.java:761)

没有字符串解析逻辑,Log完美无缺!我哪里做错了? 感谢。

1 个答案:

答案 0 :(得分:1)

嗯..按照我的要求,我会把它写成答案。

在您解析之前打印您的字符串。它可能不是你想象的那样。

一般来说,不要假设变量值和滥用print函数以及调试模式。这将告诉您程序中实际发生的情况,并防止您查找错误的错误。