android studio中的BeanShell错误消息

时间:2016-05-04 13:11:42

标签: java android beanshell

我是java的初学者,我使用Android StudioBeanshell制作了一个简单的计算器应用。当我运行该项目时,它只能在我的模拟器上正常工作。对于真实设备,此应用程序只能安装。当我尝试运行时,应用程序崩溃显示" Unfortunately 'X' app has been stopped

我的代码太简单了:

Interpreter interpreter = new Interpreter();
String equation = "2+3*9"; // I gave user to input his own via editText, 
                           // Lets think for a simple case
Object answer1 = interpreter.eval(equation);
String answer = answer1.toString();

记录:

EVENT LOG:http://pastebin.com/JwcuV0ch

GRADLE BUILD MESSAGE:http://pastebin.com/8cDXp0N5

1)您能解释一下这个错误发生的原因吗?

2)从构建消息,我粘贴了一些内容:

Error:(bsh.Interpreter$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably    produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler

我得到了行compiler that did not target the mordern .class file format 反复在我的构建消息中。所以,请告诉我如何重新编译BeanShell

[P.S. : 在Netbeans中,我编写了相同的代码来从字符串中评估值。

import bsh.Interpreter;
public class Main
{
    public static void main(String[] args)
    {
             Interpreter interpreter = new Interpreter();
             String equation = "2+3*9"; // I gave user to input his own via editText in android, 
                           // Lets think for a simple case
              String answer = interpreter.eval(equation);
              System.out.println(answer);
    }
}

那里的一切都很好,现在我很奇怪为什么相同的代码会在Android Studio]中产生问题

1 个答案:

答案 0 :(得分:1)

Interpreter.eval()返回 Object ,而不是 String 。 因此,您很可能在从 Integer 转换为 String 时遇到问题:

  

目标异常:java.lang.ClassCastException:无法强制转换   java.lang.Integer到java.lang.String

尝试将Integer返回值强制转换为String,它应该可以工作:

String answer = String.valueOf(interpreter.eval(equation));