javascript - calling activity from webview with @JavascriptInterface -


i'm stuck on project , requesting help!
i'm trying call activity(barcodescanneractivity.java) webview in mainactivity.java button click using javascript call. think messed parameters somewhere... i'm not sure i've made mistake, copied , modified codes blogs , stackoverflow.

i'm having error in android studio logcat:

[info:console(315)] "uncaught error: error calling method on npobject.", source: http:// .........

this server side html:

 <input type="button" value="scanner" id="btnscan" class="btn_dark" onclick="callactivity();" />  <script>   function callactivity() {     android.openbarcodescanner();   } </script> 

this mainactivity.java containing webview:

protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          getsupportactionbar().hide();          //getwindow().requestfeature(window.feature_action_bar);         //getactionbar().hide();          setcontentview(r.layout.activity_main);          mainwebview = (webview) findviewbyid(r.id.mainwebview);         mainwebview.setwebviewclient(new webviewclient());          //-- add javascript listener         mainwebview.addjavascriptinterface(new webappinterface(this), "android");         mainwebview.getsettings().setjavascriptenabled(true);          mainwebview.loadurl(webviewurl);     }  ..skipped brevity..  public class webappinterface {         context mcontext;          /** instantiate interface , set context */         webappinterface(context c) {             mcontext = c;         }          @javascriptinterface         public void openbarcodescanner(string str) {             intent barcodescanintent = new intent(mcontext, barcodescanneractivity.class);             mcontext.startactivity(barcodescanintent);         } } 

and last, barcodescanneractivity.java wants called:

public class barcodescanneractivity extends activity implements view.onclicklistener {  //    private static final  r = ;     // use compound button either checkbox or switch widgets work.     private compoundbutton autofocus;     private compoundbutton useflash;     private textview statusmessage;     private textview barcodevalue;      private static final int rc_barcode_capture = 9001;     private static final string tag = "barcodemain";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.barcode_scanner);          statusmessage = (textview)findviewbyid(r.id.status_message);         barcodevalue = (textview)findviewbyid(r.id.barcode_value);          autofocus = (compoundbutton) findviewbyid(r.id.auto_focus);         useflash = (compoundbutton) findviewbyid(r.id.use_flash);          findviewbyid(r.id.read_barcode).setonclicklistener(this);     }      /**      * called when view has been clicked.      *      * @param v view clicked.      */     @override     public void onclick(view v) {         if (v.getid() == r.id.read_barcode) {             // launch barcode activity.             intent intent = new intent(this, barcodecaptureactivity.class);             intent.putextra(barcodecaptureactivity.autofocus, autofocus.ischecked());             intent.putextra(barcodecaptureactivity.useflash, useflash.ischecked());              startactivityforresult(intent, rc_barcode_capture);         }      }       @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         if (requestcode == rc_barcode_capture) {             if (resultcode == commonstatuscodes.success) {                 if (data != null) {                     barcode barcode = data.getparcelableextra(barcodecaptureactivity.barcodeobject);                     statusmessage.settext(r.string.barcode_success);                     barcodevalue.settext(barcode.displayvalue);                     log.d(tag, "barcode read: " + barcode.displayvalue);                 } else {                     statusmessage.settext(r.string.barcode_failure);                     log.d(tag, "no barcode captured, intent data null");                 }             } else {                 statusmessage.settext(string.format(getstring(r.string.barcode_error),                         commonstatuscodes.getstatuscodestring(resultcode)));             }         }         else {             super.onactivityresult(requestcode, resultcode, data);         }     } } 

and 1 last question is, javascript code work in situation, too? or need modify bit?

function  callactivity(){              if(window.android){                  console.info("android");                   window.android.openbarcodescanner(number.value);                         }              else{                  console.info("web");                  document.form.submit();              }          }      } 

thank in advance!

your interface takes string parameter:

@javascriptinterface public void openbarcodescanner(string str) { 

but invocation doesn't pass parameters:

android.openbarcodescanner(); 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -