问题
import android.widget.Toast;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public void bEqual(View v) throws ScriptException {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String value = inputText.getText().toString();
Toast.makeText(this,value,Toast.LENGTH_LONG).show();
try{
result = (double)engine.eval(value);
Toast.makeText(this,String.format("%f",result),Toast.LENGTH_SHORT).show();
}catch(Exception e){
Toast.makeText(this,"Exception Raised",Toast.LENGTH_SHORT).show();
}
}
What is wrong in it? App is exiting when perform this action. It is not showing any errors but app is closing
回答1:
first add this: compile 'io.apisense:rhino-android:1.0' to your app gradle file. Then type this snippet code
TextView resultTextView = findViewById(R.id.result_text_view);
String currentText = resultTextView.getText().toString();
boolean valid = checkForValidity();
double result=0;
if(valid){
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try{
result = (double)engine.eval(currentText);
}catch(Exception e){
Toast.makeText(this,"Exception Raised",Toast.LENGTH_SHORT).show();
}
currentText = currentText +"\n"+ "="+(result);
}
resultTextView.setText(currentText);
回答2:
Easiest way is to use Mozilla Rhino in Android instead[1] because ScriptEngine and it's dependencies would require time consuming setup and headache.
Import the ff:
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import android.util.Log;
Code you put somewhere in your method:
Context context = Context.enter(); //
context.setOptimizationLevel(-1); // this is required[2]
Scriptable scope = context.initStandardObjects();
Object result = context.evaluateString(scope, "18 > 17 and 18 < 100", "<cmd>", 1, null);
Log.d("your-tag-here", "" + result);
Add it in your gradle dependencies.
implementation group: 'org.mozilla', name: 'rhino', version: '1.7.10'
Reference:
- Mozilla Rhino
- Explanation why that line of code is needed
- Mozilla Rhino's Maven Repository
回答3:
The javax.script
package is not available on Android.
If you want to use a Javascript engine, you must implement those behaviours by yourself or use one of the dependencies that already exist like rhino-android.
回答4:
javax.script not available in android that's why you have to use third party library
I am using "rhino-android" and it serve the purpose
https://github.com/APISENSE/rhino-android
implementation in gradle file
implementation 'io.apisense:rhino-android:1.0'
java code
javax.script.ScriptEngine engine = new javax.script.ScriptException().getEngineByName("rhino");
Object result = engine.eval("2+2");
回答5:
This is a real example using Rhino Script Engine in Android in Android
Use this imports:
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
and this is an example:
String Operation = "5+4-2";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try {
Object result = engine.eval(Operation);
Log.d("Calculator", "Operation: " + Operation + " result: " + result);
} catch (ScriptException e) {
Log.d("Calculator", " ScriptEngine error: " + e.getMessage());
}
Output:
Operation: 5+4-2 result: 7.0
来源:https://stackoverflow.com/questions/42128648/how-to-use-scriptenginemanager-in-android