Call javascript function from Java

前端 未结 2 1111
悲&欢浪女
悲&欢浪女 2021-01-20 07:13

I develop an app using PhoneGap. I got a service which is in background and is coded natively (the service and the phone gap app is in the same project). Unfortunatly, I wan

2条回答
  •  别那么骄傲
    2021-01-20 07:45

    Finally I found the solution : in fact the NullPointerException was on ctx. To fix it, I setup the Context value of my object c like this :

    c.setContext(use_An_Object_Of_Type_PhonegapActivity);
    

    To get the object, I grab it from the AppActivity in this method :

    public class CloudPhoneAppActivity extends DroidGap {
    private class NoScaleWebViewClient extends GapViewClient {
    
        public NoScaleWebViewClient(DroidGap ctx) {
            super(ctx);
            myApp.ctx = ctx;
        }
    
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            Log.d("NoScaleWebViewClient", "Scale changed: " + String.valueOf(oldScale) + " => " + String.valueOf(newScale));
        }
    }
    /** other stuff after**/ 
    }
    

    So finally here is the solution :

    class c = new class();
    c.setContext(myApp.ctx);
    c.execute("myFunction",null,null);
    

    There is no change to do in the execute method which is describe before. But be careful of how you call the javascript function.

    Erwan

提交回复
热议问题