我试图通过JavascriptInterface返回一个函数但是收到错误:
无法引用以不同方法定义的内部类中的非最终变量函数
这是代码:
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();
}
答案 0 :(得分:1)
错误说明了一切。变量funct在匿名事件处理程序类的范围内不可用:
应该这样做:
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();
}