I'm trying to return a function through JavascriptInterface but am getting an error:
Cannot refer to a non-final variable funct inside an inner class defined in a different method
here is the code:
public void androidFieldPrompt(String title, String msg, String funct) {
final EditText input = new EditText(MainActivity.this);
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle(title);
alert.setMessage(msg);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
webView.loadUrl("javascript:window."+funct+"('"+value+"')");
return;
}
});
alert.setNegativeButton("CANCEL", null);
alert.show();
}
The error says it all. The variable funct is not available in the scope of the anonymous event handler class:
That should do the trick:
public void androidFieldPrompt(String title, String msg, final String funct) {
final EditText input = new EditText(MainActivity.this);
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle(title);
alert.setMessage(msg);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
webView.loadUrl("javascript:window."+funct+"('"+value+"')");
return;
}
});
alert.setNegativeButton("CANCEL", null);
alert.show();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.