图片包含javascript没有出现在webView中

时间:2010-01-03 10:20:38

标签: javascript android webview

我用webView来显示javaScript,javaScrip中包含了一个图像,但webView没有运行图像,反而出现了“?”图像。 Idonot为什么? 我的代码:

demo.html

<html>  
    <script language="javascript">  
        /* This function is invoked by the activity */   
        function wave() {   

            document.getElementById("droid").src="android_waving.png";   
            alert("2");   
        }   
    </script>  
    <body>  
        <!-- Calls into the javascript interface for the activity -->  
        <a onClick="window.demo.clickOnAndroid()"><div style="width:80px;   
            margin:0px auto;   
            padding:10px;   
            text-align:center;   
            border:2px solid #202020;" >  
                <img id="droid" src="android_normal.png"/><br>  
                Click me!   
        </div></a>  
    </body>  
</html>  
public class webJsDemo extends Activity {
    /** Called when the activity is first created. */
    private WebView mWebView;
     private Handler mHandler = new Handler();   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mWebView = (WebView) findViewById(R.id.wv1);   

        WebSettings webSettings = mWebView.getSettings();   
        webSettings.setSavePassword(false);   
        webSettings.setSaveFormData(false);   
        webSettings.setJavaScriptEnabled(true);   
        webSettings.setSupportZoom(false);   

        mWebView.setWebChromeClient(new MyWebChromeClient());   

        mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");   

        mWebView.loadUrl("file:///android_asset/demo.html");   
    }   

    final class DemoJavaScriptInterface {   

        DemoJavaScriptInterface() {   
        }   

        /**  
         * This is not called on the UI thread. Post a runnable to invoke  
         * loadUrl on the UI thread.  
         */  
        public void clickOnAndroid() {  

            mHandler.post(new Runnable() {   
                public void run() {   
                    mWebView.loadUrl("javascript:wave()");   
                }   
            });   

        }   
    }   

    /**  
     * Provides a hook for calling "alert" from javascript. Useful for  
     * debugging your javascript.  
     */  
    final class MyWebChromeClient extends WebChromeClient {   
        @Override
        public boolean onJsAlert(WebView view, String url, String message,
                JsResult result) {
            // TODO Auto-generated method stub
            Log.d("aa", message);  
            Log.d("url", url);
            Log.d("result", ""+result);
            result.confirm(); 
        return super.onJsAlert(view, url, message, result);

        }


    }   
}  

我把android_normal.png和android_waving.png放在资产和可绘制文件夹中

the result

1 个答案:

答案 0 :(得分:1)

您是否尝试将图片放在html文件所在的位置?