2013-07-13 7 views
9

アクティビティからjavascript関数を呼び出していますが、私はUncaught ReferenceError: myFunction is not defined at null:1エラーが発生しています。ここに私のファイルは、私は私がwebview.loadUrl("javascript:myFunction()");を使用していたときにこのエラーが発生して見てきました未知の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> 

です。私はjavaからxml文字列を渡し、javascriptに解析したいと思います。解決策を見つけるのを手伝ってください。

答えて

17

ページは、コードが実行されると、あなたのjavascript機能を見つけます

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

にロードされたときには、JavaScriptを実行する必要があります。あなたが今やっているやり方は待たない。

+0

を設定したパラメータを変更する必要が働いていた私も、これを試してみましたが、それはエラーが表示されないが、何の機能を呼び出していないにも警告メッセージが表示。 –

+0

JS機能のコンソールログを記録し、何が起きているかを確認します。また、なぜforループからアラートを表示していますか? – Rajeev

+0

これは、JSを呼び出す方法です。エラーは、maknoteライブラリまたはJS関数の一部にあります。 – AlexBcn

0

この1つは、単にシーケンス

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

関連する問題