2012-02-06 3 views
0

아래 코드가 JavaScriptInterface를 트리거하지 않는 이유 : 웹보기가로드 될 때 대화 상자 팝업이 표시되지 않습니다. (android.widget.Toast가해야한다고 생각합니다.)JavaScriptInterface가 호출되지 않았습니다.

package com.example; 

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebView; 

public class MyActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     String data = "<html>" + 
       "<body><h1>Test JavaScriptInterface</h1></body>" + 
       "<script>Android.showToast(toast);</script>" + 
       "</html>"; 

     WebView engine = (WebView) findViewById(R.id.web_engine); 
     engine.getSettings().setJavaScriptEnabled(true); 
     engine.addJavascriptInterface(new JavaScriptInterface(this), "Android"); 
     engine.loadData(data, "text/html", "UTF-8"); 

    } 
} 

package com.example;

import android.content.Context; 
import android.widget.Toast; 

public class JavaScriptInterface { 

    Context mContext; 

    /** Instantiate the interface and set the context */ 
    JavaScriptInterface(Context c) { 
     mContext = c; 
    } 

    /** Show a toast from the web page */ 
    public void showToast(String toast) { 
     Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
    } 

} 

답변

1

나는 분명히 틀린 것을 보지 못했기 때문에 단지 추측 할 만하다. 이 변경 :

String data = "<html>" + 
      "<body><h1>Test JavaScriptInterface</h1></body>" + 
      "<script>Android.showToast(toast);</script>" + 
      "</html>"; 

이에 :

String data = "<html>" + 
      "<body><h1>Test JavaScriptInterface</h1></body>" + 
      "<script>Android.showToast('toast');</script>" + 
      "</html>"; 
+0

을 네 덕분에 내가 구문 너무 바보 해요 :) – user310291

관련 문제