我正在尝试将类导入MVEL的实现,我收到以下错误:
[错误:com.MyApp.Main类型的预期接收者,但得到了java.lang.Class]
以下是设置MVEL的方法:
public void runCode() {
final String theCode = "Main.sendText();";
System.setProperty("java.version", "1.6");
new Thread(new Runnable() {
public void run() {
ParserContext context = new ParserContext();
context.addImport("Main", Main.class);
Serializable compiled = MVEL.compileExpression(theCode, context); // compile the expresion
HashMap vars = new HashMap();
vars.put("x", new Integer(10));
vars.put("y", new Integer(10));
Integer result = (Integer) MVEL.executeExpression(compiled, vars);
}
}).start();
}
Main.class:
public void sendText(){
System.out.println("Success!");
}
logcat的:
08-05 20:23:31.900: E/AndroidRuntime(19968): FATAL EXCEPTION: Thread-30751
08-05 20:23:31.900: E/AndroidRuntime(19968): [Error: expected receiver of type com.MyApp.Main, but got java.lang.Class<com.MyApp.Main>]
08-05 20:23:31.900: E/AndroidRuntime(19968): [Near : {... Main.sendText(); ....}]
08-05 20:23:31.900: E/AndroidRuntime(19968): ^
08-05 20:23:31.900: E/AndroidRuntime(19968): [Line: 1, Column: 1]
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:435)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:143)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.optimizers.dynamic.DynamicOptimizer.optimizeAccessor(DynamicOptimizer.java:67)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.MVEL.executeExpression(MVEL.java:969)
08-05 20:23:31.900: E/AndroidRuntime(19968): at com.MyApp.KessilLightScenes$1.run(KessilLightScenes.java:136)
08-05 20:23:31.900: E/AndroidRuntime(19968): at java.lang.Thread.run(Thread.java:856)
08-05 20:23:31.900: E/AndroidRuntime(19968): Caused by: java.lang.IllegalArgumentException: expected receiver of type com.MyApp.Main, but got java.lang.Class<com.MyApp.Main>
08-05 20:23:31.900: E/AndroidRuntime(19968): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 20:23:31.900: E/AndroidRuntime(19968): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:1104)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:987)
08-05 20:23:31.900: E/AndroidRuntime(19968): at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:344)
08-05 20:23:31.900: E/AndroidRuntime(19968): ... 11 more
知道如何修复此错误吗?感谢
答案 0 :(得分:6)
听起来您尝试访问的方法可能需要是静态的。