2016-07-14 44 views
0

這裏傳遞一個值(字符串)Webview是我MainActivity.java我想從我的活動javaclass在android系統

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.webkit.JavascriptInterface; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    JavaScriptInterface JSInterface; 
    JavaScriptInterface StringGetter; 
    String key; 
    EditText urlField; 
    ImageButton goButton; 
    WebView webView; 
    String url; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     String key = "sending values to javascript"; 
     url = "file:///android_asset/Index.html"; 
     webView = (WebView) findViewById(R.id.webView); 
     webView.setWebViewClient(new MyBrowser()); 
     StringGetter = new JavaScriptInterface(this); 
     webView.addJavascriptInterface(StringGetter, "AndroidFunction"); 
     webView.getSettings().setLoadsImagesAutomatically(true); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
     webView.loadUrl(url); 
    } 

    public class StringGetter { 
     Context jContext; 
     StringGetter(Context context) { 
      jContext = context; 
     } 
     @JavascriptInterface 
     public void getString() { 
      webView.loadUrl("javascript:callFromActivity('" + key + "');"); 
     } 
    } 

    private class MyBrowser extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    } 
} 

,這裏是我的HTMLFILE

<!DOCTYPE html> 


<html lang="en" xmlns="http://www.w3.org/1999/html"> 

<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
    <title>Bootstrap 101 Template</title> 

    <!-- Bootstrap --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
    <!--[if lt IE 9]> 
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
      <![endif]--> 


    <script type="text/javascript"> 
     function showAndroidToast() { 
      JSInterface.AlertDisplay(); 
     } 

     function myMethod() { 
      alert("Hello world"); 
      AndroidFunction.getString(); 
     } 

     function callFromActivity(msg) { 

      document.getElementById("btn").innerHTML = msg; 
      alert(msg.anchor("val")); 
     } 
    </script> 
</head> 

<body onload="myMethod()"> 
    <nav class="navbar navbar-default navbar-fixed-top"> 

     <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse" aria-expanded="false"> 
      <span class="sr-only">Toggle navigation</span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
     </button> 

     <input type="button" id="btn" value=" Check " onClick="showAndroidToast()" /> 



     <a class="navbar-brand" href="#">Some Name </a> 
     <div class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav navbar-right"> 
       <li class="active"><a href="#">Home</a> 
       </li> 
       <li class="active"><a href="#">About</a> 
       </li> 
       <li class="active"><a href="#">Downloads</a> 
       </li> 
       <li class="active"><a href="#">Contact</a> 
       </li> 
       <li> 
        <div class="dropdown"> 
         <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 
          Dropdown 
          <span class="caret"></span> 
         </button> 
         <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> 
          <li><a href="#">Action</a> 
          </li> 
          <li><a href="#">Another action</a> 
          </li> 
          <li><a href="#">Something else here</a> 
          </li> 
          <li role="separator" class="divider"></li> 
          <li><a href="#">Separated link</a> 
          </li> 
         </ul> 
        </div> 
       </li> 
      </ul> 
     </div> 
    </nav> 

    <div class="container"> 
     <div class="jumbotron text-center"> 
      <div class="container-fluid"> 

       <div class="row"> 
        <div class="col-md-3">ColUMN 1 contains some text and other components to work with </div> 
        <div class="col-md-3">COLUMN 2 contains some text and other components to work with </div> 
        <div class="col-md-3">ColUMN 3 contains some text and other components to work with </div> 
        <p id="p">hello</p> 
       </div> 
      </div> 
     </div> 
    </div> 

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.min.js"></script> 
</body> 

</html> 

我得到以下錯誤(沒有任何變化,也沒有警報顯示):

07-14 17:22:05.808 31172-31172/com.example.app.webview I/chromium: [INFO:CONSOLE(28)] "Uncaught TypeError: AndroidFunction.getString is not a function", source: file:///android_asset/Index.html (28)

回答

0

您的代碼看起來非常適合我。請嘗試從任何其他輸入元素說按鈕或文本框,而不是onload()調用myMethod()

+0

我試過了,但是同樣的事情。即使警報功能不工作,頁面也沒有響應。 –