Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android通过JavascriptInterface返回函数_Javascript_Android - Fatal编程技术网

android通过JavascriptInterface返回函数

android通过JavascriptInterface返回函数,javascript,android,Javascript,Android,我试图通过JavascriptInterface返回函数,但出现错误: 不能引用在不同方法中定义的内部类中的非最终变量funct 代码如下: public void androidFieldPrompt(String title, String msg, String funct) { final EditText input = new EditText(MainActivity.this); AlertDialog.B

我试图通过JavascriptInterface返回函数,但出现错误:

不能引用在不同方法中定义的内部类中的非最终变量funct

代码如下:

         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();            
         }

错误说明了一切。变量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();            
     }

我在使用最终var:E/Web控制台(26069)的生产中遇到了此错误:未捕获的TypeError:object[object DOMWindow]的属性“undefined”不是null处的函数:1@ushdiver在加载URL之前添加日志输出。类似于Log.d(“ASDF”,“My funct:+funct”);可能该值为null。我是通过该方法发送的,而不仅仅是javascript端AndroidFieldCompt(“”,'Search the Help Database',promptResult);-已更改为AndroidFieldCompt(“”,'SearchtheHelpDatabase','promptResult')。决赛结束后,我们成功了