在Android 2.2上使用Rhino 1.7 r2尝试将对象添加到JS环境时,我总是得到一个NullPointerException。我已经将课程简化到最低限度以获得此异常,我无法弄清楚原因。这是始终产生异常的代码:
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
以外的任何其他内容,并且不会发生此问题。
这是stacktrace:
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
对于为什么会发生这种情况的任何见解都将非常感激。
[编辑] 经过一些严肃的谷歌搜索和头部粉碎后,我发现了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 );
}
}
答案 0 :(得分:2)
我试过你的两个例子: