本文介绍了错误:类型为com.MyApp.Main的预期接收者,但收到了java.lang.Class< com.MyApp.Main>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将类导入MVEL的实现中,并且出现以下错误:
I'm trying to import a class into an implementation of MVEL, and I get the following error:
[错误:类型为com.MyApp.Main的预期接收者,但得到了java.lang.Class]
[Error: expected receiver of type com.MyApp.Main, but got java.lang.Class]
以下是设置MVEL的方法:
Here's the method that sets up 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:
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
任何想法如何解决此错误?谢谢
Any idea how to fix this error? Thanks
推荐答案
听起来您尝试访问的方法可能需要是静态的.
It sounds like the method(s) you're attempting to access may need to be static.
这篇关于错误:类型为com.MyApp.Main的预期接收者,但收到了java.lang.Class< com.MyApp.Main>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!