gpt4 book ai didi

java - 在 Android 上嵌入 Rhino 的问题

转载 作者:行者123 更新时间:2023-11-29 22:30:30 24 4
gpt4 key购买 nike

当尝试在 Android 2.2 上使用 Rhino 1.7 r2 将对象添加到 JS 环境时,我总是得到 NullPointerException。我已将类(class)简化到最低限度以获得此异常,但我不明白为什么。这是总是产生异常的代码:

public class StartActivity extends Activity
{
/** Rhino context object */
private Context mCX;
/** Rhino script scope object */
private Scriptable mScope;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// Create our context and turn off compilation
mCX = Context.enter();
mCX.setOptimizationLevel( -1 );

// Initialize the scope
mScope = mCX.initStandardObjects();

Object insertObj;

// This line always throws the exception.
insertObj = Context.javaToJS( this, mScope );

// We never get here
ScriptableObject.putProperty( mScope, "platform", insertObj );

setContentView(R.layout.main);
}
}

将麻烦的行更改为其他内容,如 insertObj = Context.javaToJS( null, mScope ); 或除 this 以外的任何内容,问题就不会发生。

这是堆栈跟踪:

E/AndroidRuntime(  308): FATAL EXCEPTION: main
E/AndroidRuntime( 308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me/com.me.StartActivity}: java.lang.NullPointerException
E/AndroidRuntime( 308): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 308): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 308): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 308): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 308): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 308): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 308): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 308): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 308): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 308): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 308): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 308): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 308): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 308): at org.mozilla.javascript.net.sf.retrotranslator.runtime.impl.MethodDescriptor.getInstance(MethodDescriptor.java:137)
E/AndroidRuntime( 308): at org.mozilla.javascript.net.sf.retrotranslator.runtime.java.lang.reflect._Constructor.isVarArgs(_Constructor.java:83)
E/AndroidRuntime( 308): at org.mozilla.javascript.jdk15.VMBridge_jdk15.isVarArgs(VMBridge_jdk15.java:66)
E/AndroidRuntime( 308): at org.mozilla.javascript.MemberBox.init(MemberBox.java:86)
E/AndroidRuntime( 308): at org.mozilla.javascript.MemberBox.<init>(MemberBox.java:72)
E/AndroidRuntime( 308): at org.mozilla.javascript.JavaMembers.reflect(JavaMembers.java:667)
E/AndroidRuntime( 308): at org.mozilla.javascript.JavaMembers.<init>(JavaMembers.java:76)
E/AndroidRuntime( 308): at org.mozilla.javascript.JavaMembers.lookupClass(JavaMembers.java:838)
E/AndroidRuntime( 308): at org.mozilla.javascript.NativeJavaObject.initMembers(NativeJavaObject.java:90)
E/AndroidRuntime( 308): at org.mozilla.javascript.NativeJavaObject.<init>(NativeJavaObject.java:80)
E/AndroidRuntime( 308): at org.mozilla.javascript.NativeJavaObject.<init>(NativeJavaObject.java:70)
E/AndroidRuntime( 308): at org.mozilla.javascript.WrapFactory.wrapAsJavaObject(WrapFactory.java:149)
E/AndroidRuntime( 308): at org.mozilla.javascript.WrapFactory.wrap(WrapFactory.java:105)
E/AndroidRuntime( 308): at org.mozilla.javascript.Context.javaToJS(Context.java:1698)
E/AndroidRuntime( 308): at com.me.android.StartActivity.onCreate(StartActivity.java:34)
E/AndroidRuntime( 308): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 308): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 308): ... 11 more

任何有关为什么会发生这种情况的见解都将不胜感激。

[编辑] 经过一番认真的谷歌搜索和头脑 Storm 后,我发现了 ScriptableObject.defineClass 方法并尝试使用它,按照 Rhino 文档中的示例,但是现在我收到 RuntimeException“ReferenceError:‘DeviceInterface’未定义。”我知道这是可能的,因为 Rhino 就是 ASE用于其 JavaScript 支持。

这是抛出 RuntimeException 的新代码:

public class StartActivity extends Activity
{
/** Rhino context object */
private Context mCX;
/** Rhino script scope object */
private Scriptable mScope;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// Create our context and turn off compilation
mCX = Context.enter();
mCX.setOptimizationLevel( -1 );

// Initialize the scope
mScope = mCX.initStandardObjects();

try{
ScriptableObject.defineClass( mScope, DeviceInterface.class );
} catch( Exception e ) {
Log.e("StartActivity", e.toString());
}

// This is the line that throws the exception
mCX.evaluateString(mScope,
"var p = new DeviceInterface();", "",
1, null);

setContentView(R.layout.main);
}
}

还有 DeviceInterface 类:

public class DeviceInterface extends ScriptableObject
{
static private final long serialVersionUID = 1L;

static public void init ( Scriptable scope )
{
Log.i( "JSInterface", "Init called" );
}

public DeviceInterface()
{
logger( "Created new JSInterface Object" );
}

@Override
public String getClassName()
{
return "DeviceInterface";
}

public void jsFunction_logger( String message )
{
logger( message );
}

private void logger( String message )
{
if( message.contains("[alrt]") )
Log.e( "JSInterface", message );
else if( message.contains("[info]") )
Log.i( "JSInterface", message );
else
Log.d( "JSInterface", message );
}
}

最佳答案

我试过你的两个例子:

  • 您的第一个示例在我的项目中无任何异常地执行(在带有 ADK 插件的 Eclipse 下的 Debug模式下)- 您是如何嵌入 Rhino 库的?我对“配置构建路径…/添加外部 JAR…”感到满意
  • 您的第二个示例在标准 JVM 上抛出相同的异常 - 这意味着它不是 Android 问题。

关于java - 在 Android 上嵌入 Rhino 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4230740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com