ScriptEngine in android studio

480 Views Asked by At

I am making a calculator. So here i successfully imported Script Engine.

So now I stored every value on the r_text variable. Ex. String r_text = "10 + 10"

And here the tview is the TextView

Equal button script:

but19.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View view){
        
          String r_text = tview.getText().toString();
          
          if (r_text.contains("×")){
            r_text = r_text.replace("×", "*");
          }
          if (r_text.contains("÷")){
            r_text = r_text.replace("÷", "/");
          }
          
          String result = ""; 
          
          ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
          
          try{
            Object ff = engine.eval(r_text);
            result = ff.toString();
          }catch (ScriptException e)
          {
            ;
          }
          if (result != ""){
            tview.setText(result);
          }
        }
        
      });

But after baking the calculator when I clicked on equal button app automatically crashed. I don't know why please help.

And also app successfully installing on my mobile. (I can't run it on virtual emulator cuz my computer is kinda slow. So I don't understand how to view application error log after it crashed.)

1

There are 1 best solutions below

0
moh ash On

There is no ScriptEngineManager or ScriptEngine classes in android sdk. cant import them from javax. and that's really sad.

you have to manipulate the arithmetic string yourself.