【问题标题】:(Android) ANTLRStringStream throwing exception(Android) ANTLRStringStream 抛出异常
【发布时间】:2013-05-01 19:57:01
【问题描述】:

我一直在尝试让 ANTLR-3.5-complete.jar 在 Android 应用程序上运行,但我无法让它运行! Bart Kiers 的回答 HERE 对我帮助很大,但我仍然无法正常工作。

我正在尝试制作一个按钮,当单击该按钮时,它将根据我的 grammar.g 验证 EditText 视图中的文本是否正确,但文本是否无关紧要我输入是否正确,当我在实例化 ANTLRStringStream 时单击按钮时,我的应用程序总是崩溃。这是我所拥有的:

public void onClickVerify(View v) throws TypeNotPresentException {
    String source = "foobar";
    Log.e("TestDebug", "Instantiating views");
    TextView out = (TextView) findViewById(R.id.textView1);
    EditText in = (EditText) findViewById(R.id.editText1);
    try {
        Log.e("TestDebug", "Getting source from EditText");
        source = in.getText().toString();
        Log.e("TestDebug", "source = " + source);
        Log.e("TestDebug", "Instantiating stream");
        ANTLRStringStream stream = new ANTLRStringStream(source);
        Log.e("TestDebug", "Instantiating lexer");
        grammarLexer lexer = new grammarLexer(stream);
        Log.e("TestDebug", "Instantiating parser");
        grammarParser parser = new grammarParser(
                new BufferedTokenStream(lexer));
        Log.e("TestDebug", "Applying rule to parser");
        out.setText(source + " = " + !parser.failed());
    } catch (IllegalStateException ise) {
        out.setText("Exception caught");
    }
}

这是我在日志中看到的:

  • 实例化视图
  • 从 EditText 获取源代码
  • 来源 = test33
  • 实例化流

然后“不幸的是,TestAntlrApp 已停止”。弹出窗口。

编辑:这是我的更多日志。

java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
... 11 more
Caused by: java.lang.NoClassDefFoundError: org.antlr.runtime.ANTLRStringStream
at com.test.antlr.MainActivity.onClickVerify(MainActivity.java:89)

关于找不到类 ANTLRStringStream 类的问题?然而,代码中似乎没有错误/警告,我什至可以看到类的层次结构。

【问题讨论】:

  • 如果您使用上一个答案中提到的 ANTLR 版本会怎样?
  • 抛出了什么异常?用这个替换你的 catch 并发布你在日志猫上看到的内容:catch (Exception e) { Log.e("TestDebug", "Exception caught: ", e); }
  • @BartKiers 我会尝试更改 ANTLR 版本,看看会发生什么!我更改了catch,但代码永远不会被执行。它在try 内崩溃而从未到达catch,因此我的日志输出保持不变。编辑:v3.3 似乎已从 ANTLR 网站中删除。我只能找到 v3.5。
  • 网站从 antlr.org 更改为 antlr3.org。所以现在的链接是:antlr3.org/download/antlr-3.3-complete.jar
  • @BartKiers 谢谢!我用 v3.3 试过,但我仍然遇到同样的问题。该应用程序在实例化 stream 时简单地死掉了...我找不到可能的解释来解释为什么会发生这种情况。

标签: android stream antlr


【解决方案1】:

由于最近的 ADT 更新,您不能只使用 Eclipse 的传统方式将外部库添加到构建路径。相反,您必须将它放在 libs 文件夹中才能将其打包到 .apk 中。如果您只是将它添加到构建路径中,那么在构建 .apk 时它将被忽略。

症状:应用无法识别外部库类。

解决方法:将 .jar 文件放到 libs 文件夹中。

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 2012-09-30
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2016-02-08
    • 2013-02-14
    • 1970-01-01
    相关资源
    最近更新 更多