2013-07-13 34 views
9

我从一个活动调用javascript函数,但出现Uncaught ReferenceError: myFunction is not defined at null:1错误。下面是我的文件未捕获的ReferenceError:myFunction未定义为null:1 webview中的Android异常

MainActivity.java

package com.example.androidexample; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.Menu; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     WebView webview = (WebView)this.findViewById(R.id.webView1); 
     WebSettings webSettings = webview.getSettings(); 

     // Enable Javascript for interaction 
     webSettings.setJavaScriptEnabled(true); 

     // Make the zoom controls visible 
     webSettings.setBuiltInZoomControls(true); 

     // Allow for touching selecting/deselecting data series 
     webview.requestFocusFromTouch(); 

     // Set the client 
     webview.setWebViewClient(new WebViewClient()); 
     webview.setWebChromeClient(new WebChromeClient()); 


     //webview.loadUrl("file:///android_asset/test.html"); 

     /*String str = "<books>" + 
       "<book title=\"A Tale of Two Cities\"/>" + 
       "<book title=\"1984\"/>" + 
       "</books>";*/ 




     webview.loadUrl("file:///android_asset/test.html"); 
     webview.loadUrl("javascript:myFunction()"); 
     //webview.loadUrl("javascript:myFunction("+str+")"); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    }  
} 

的test.html

<html> 
<body> 
<script type="text/javascript" src="marknote.js"></script> 
<script type="text/javascript"> 
function myFunction() 
{ 
var str = '<books><book></book></books>'; 
var parser = new marknote.Parser(); 
var doc = parser.parse(str); 

var bookEls = doc.getRootElement().getChildElements(); 

    for (var i=0; i<bookEls.length; i++) { 
     var bookEl = bookEls[i]; 

     alert("Element name is " + bookEl.getName()); 
    } 
} 
</script> 

</body> 
</html> 

我所看到的,当我使用webview.loadUrl("javascript:myFunction()");这个错误发生。我想从java传递一个xml字符串并将其解析为javascript。请帮我找到一个解决方案。

回答

17

当页面加载

webview.setWebViewClient(new WebViewClient(){ 
    public void onPageFinished(WebView view, String url){ 
     webview.loadUrl("javascript:myFunction()"); 
    }   
}); 

的代码会被执行,会发现你的JavaScript功能,您应该执行JavaScript。你现在的做法不会等待。

+0

我也试过,但它没有显示错误,但没有功能呼唤也没有警报消息显示。 –

+0

将控制台日志放入JS函数中,并检查发生了什么。另外,你为什么要显示for循环的警报? – Rajeev

+0

这是JS应该被调用的方式,错误发生在你的maknote库或JS函数的某个部分。 – AlexBcn

0

这一个工作只需要改变参数设置序列

package com.example.androidexample; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.Menu; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final WebView webview = (WebView)this.findViewById(R.id.webView1); 
     WebSettings webSettings = webview.getSettings(); 

     // Enable Javascript for interaction 
     webSettings.setJavaScriptEnabled(true); 

     // Make the zoom controls visible 
     webSettings.setBuiltInZoomControls(true); 

     // Allow for touching selecting/deselecting data series 
     webview.requestFocusFromTouch(); 

     // Set the client 
//  webview.setWebViewClient(new WebViewClient()); 
//  webview.setWebChromeClient(new WebChromeClient()); 




     final String str = "<books>" + 
       "<book title=\"A Tale of Two Cities\"/>" + 
       "<book title=\"1984\"/>" + 
       "</books>"; 



//  webview.loadUrl("file:///android_asset/test.html"); 

     webview.setWebChromeClient(new WebChromeClient()); 
     webview.setWebViewClient(new WebViewClient() 
     { 

      public void onPageFinished(WebView view, String url) 
      { 
       webview.loadUrl("javascript:myFunction('"+str+"')"); 
      } 
     } 
      ); 
     webview.loadUrl("file:///android_asset/test.html"); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    }  
} 
+0

不明白。有什么不同? – soommy12