简体   繁体   English

WebView和shouldOverrideUrlLoading

[英]WebView and shouldOverrideUrlLoading

My code: 我的代码:

    public class View_WebView extends Activity  {
    WebView myWebView;

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.endsWith(".mp4")){
            Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
            startActivity(in);
            return true;
        }
        else
            return false;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());      
        myWebView.loadUrl("http://hdcast.pl");
    }     

}


    <?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 />

Eclipse compiles correctly, when I launch my application on my smartphone I got an error: Eclipse编译正确,当我在智能手机上启动应用程序时出现错误:

"Unfortunately, the application XX has been stopped" “不幸的是,申请XX已被停止”

Your webView is null, uncomment this line: 您的webView为null,取消注释此行:

myWebView = (WebView) findViewById(R.id.webview);

Edit: 编辑:

You also must have a WebView element in your XML 您还必须在XML中具有WebView元素

 <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 />

You are not using it correctly! 你没有正确使用它!

You have to make your own custom WebClient that will extend the WebClient and inside there make use of shouldOverrideUrlLoading under your needs. 您必须创建自己的自定义WebClient,它将扩展WebClient,并在其中根据您的需要使用shouldOverrideUrlLoading。

http://developer.android.com/guide/webapps/webview.html http://developer.android.com/guide/webapps/webview.html

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".mp4")){
        Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
        startActivity(in);
        return true;
    }
    else
        return false;
    }
}

Then you add your custom WebClient to your WebView and you are good to go. 然后将自定义WebClient添加到WebView中,您就可以开始使用了。

myWebView.setWebViewClient(new MyWebViewClient());      
myWebView.loadUrl("XXX");

EDIT:As per the previous answer though, make sure that you are initializing your WebView with findViewById() 编辑:根据前面的答案,请确保您使用findViewById()初始化WebView

Do it directly : 直接做:

From this tutorial 从本教程

myWebView.setWebViewClient(new WebViewClient() {  
@Override  
public boolean shouldOverrideUrlLoading(WebView view, String url) {  
 if (url.endsWith(".mp4")){
    Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
    startActivity(in);
    return true;
}
else
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM