0
    package com.webview;

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;


    public class Webview_integrationActivity extends Activity {


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

            WebView mywebview = (WebView) findViewById(R.id.webView1);
            mywebview.setWebViewClient(new MyWebViewClient());



        WebViewClient client = new MyWebViewClient()
        {
            @Override
            public void launchExternalBrowser(String url) {
                 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                 startActivity(intent);
            }

        };
    }
    }


***************************************************************************************


package com.webview;


import android.content.Intent;
import android.net.Uri;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public abstract  class MyWebViewClient extends WebViewClient {

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.google.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        launchExternalBrowser(url);    

        return true;

}

    public abstract void launchExternalBrowser(String url);



}

**当我运行此程序时,我收到错误消息“无法实例化 MyWebViewClient 类型”。我知道那是因为该类是抽象的。但是如何解决我的问题。我已经粘贴了两个类的代码使用。

4

2 回答 2

0

第一:你不能实例化一个抽象类。你只能扩展一个抽象类。

第二:创建自己的 WebViewClient 类(MyWebViewClient)需要什么。您可以在主要活动中使用 webViewClient 覆盖 shouldOverrideUrlLoading(WebView view, String url) 方法,并在此方法中执行与您在 launchExternalBrowser(String url) 方法中执行的相同操作。

我的意思是你在 launchExternalBrowser(String url) 方法中触发了一个意图,该方法是从 shouldOverrideUrlLoading(WebView view, String url) 方法调用的。那么为什么不在 shouldOverrideUrlLoading(WebView view, String url) 方法本身中触发相同的 Intent。

于 2012-07-03T12:06:08.200 回答
-4
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;


public class Webview_integrationActivity extends Activity {


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

        final WebView mywebview = (WebView) findViewById(R.id.webView1);
        mywebview.setWebViewClient(new WebViewClient());

        Button button1 = (Button) findViewById(R.id.My_btn);
        button1.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {



                          startActivity(new Intent(
                                       "android.intent.action.VIEW",
                                        Uri.parse("https://www.google.com")));
                           // TODO Auto-generated method stub
                          }

              });

    }
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.google.com")) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

            startActivity(intent);

            return true;

    }

}
于 2012-07-04T10:44:19.610 回答