7

我正在调查de.undercouch:citeproc-java:2.0.0我当前的 Android 应用程序。

我无法识别ScriptEngine适用于 Android 的citeproc-java.

我的 Gradle 类似于:-

ext {
    compileSdkVersion = 30
    minSdkVersion = 26
    targetSdkVersion = 30
}

dependencies {

    implementation 'io.apisense:rhino-android:1.1.1'

    implementation 'de.undercouch:citeproc-java:2.0.0'
    implementation 'org.citationstyles:styles:20.11'
    implementation 'org.citationstyles:locales:20.11'

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.10"
}

Citeproc要求javax.script.ScriptEngineManager在 Android 平台上不可用,因此我已替换io.apisense:rhino-android:1.1.1

我现在正面临这个例外......

2020-11-19 10:02:22.325 4426-4426/com.google.android.gms.location.sample.basiclocationsample E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.google.android.gms.location.sample.basiclocationsample, PID: 4426
    java.lang.RuntimeException: Unable to start activity ComponentInfo{----}: java.lang.IllegalArgumentException: Could not make bibliography
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IllegalArgumentException: Could not make bibliography
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:797)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750)
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86)
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: de.undercouch.citeproc.script.ScriptRunnerException: Could not call method
        at de.undercouch.citeproc.script.JREScriptRunner.callMethod(JREScriptRunner.java:87)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:784)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764) 
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750) 
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86) 
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66) 
        at android.app.Activity.performCreate(Activity.java:7802) 
        at android.app.Activity.performCreate(Activity.java:7791) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: javax.script.ScriptException: org.mozilla.javascript.EcmaError: TypeError: Cannot read property "strings" from undefined (<Unknown source>#14312) in <Unknown source> at line number 14312
        at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:330)
        at com.sun.script.javascript.RhinoScriptEngine.invokeMethod(RhinoScriptEngine.java:296)
        at de.undercouch.citeproc.script.JREScriptRunner.callMethod(JREScriptRunner.java:84)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:784) 
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764) 
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750) 
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86) 
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66) 
        at android.app.Activity.performCreate(Activity.java:7802) 
        at android.app.Activity.performCreate(Activity.java:7791) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot read property "strings" from undefined (<Unknown source>#14312)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4198)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4176)
        at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4209)
        at org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:4228)
        at org.mozilla.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:4240)
        at org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1570)
        at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1336)
        at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:911)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
        at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:146)
        at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:139)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3508)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:107)
2020-11-19 10:02:22.325 4426-4426/com.google.android.gms.location.sample.basiclocationsample E/AndroidRuntime:     at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:324)
            ... 22 more

导致此异常的 citeproc 代码直接取自他们的示例...

private fun what() {

    val citeproc = CSL(MyItemProvider(), "ieee")
    citeproc.setOutputFormat("text")
    citeproc.registerCitationItems("ID-1", "ID-2", "ID-3")

    val s1 = citeproc.makeCitation("ID-1")
    println(s1[0].text)
    //=> [1] (for the "ieee" style)

    //=> [1] (for the "ieee" style)
    val s2 = citeproc.makeCitation("ID-2")
    println(s2[0].text)
    //=> [2]

    //=> [2]
    val bibl = citeproc.makeBibliography()  //EXCEPTION RAISED HERE!!!!!!
    for (entry in bibl.entries) {
        println(entry)
    }

}

我创建了一个 AndroidJUnit 来调查这个问题。

在单步执行代码时,我发现问题是 arrayOutOfBoundsException 访问字符串 []

该数组只有 12 个条目,但索引为 39

如 AS 图像所示

在此处输入图像描述

4

1 回答 1

2

正如我在调试器中看到的值,它在iCode[] array 中显示了 12 个项目。而from.pc的值为 39,这是数组索引异常的原因。

正如您的代码from.pc值通过一个条件预先增加而增加,而在其他值中通过后增加每次增加 2 增加。

而且您使用continue时不检查总是会创建异常的数组的大小。

您应该检查您的条件,因为如果第 39 位的预期值或索引增量不正确,看起来数组确实已正确初始化。

于 2020-11-26T17:31:23.207 回答