我一直在尝试让 ANTLR-3.5-complete.jar 在 Android 应用程序上工作,但我无法让它工作!Bart Kiers 的回答HERE帮助了我很多,但我仍然无法让它发挥作用。
我正在尝试制作一个按钮,单击该按钮时,将根据我的语法验证 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 类的东西?然而,代码中似乎没有错误/警告,我什至可以看到类的层次结构。