2012-07-03 62 views
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); 



} 

**我收到錯誤當我運行這個program.I知道那是因爲該類是abstract.But如何解決我的issue.I「無法實例類型MyWebViewClient」已經粘貼了我正在使用的兩個類的代碼。無法實例類型MyWebViewClient

+0

我正在做這個項目來處理頁面導航,以便鏈接在我的webview中打開。 –

+0

我認爲你沒有讀完整個代碼。我已經用過了。 –

+0

請閱讀第二堂課。 ****************************** –

回答

-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; 

    } 

} 
+0

我解決了這個問題。 –

+2

這不是一種增加聲譽的方法。你所做的是,你只是首先接受我的答案,然後在接下來的幾天內寫下你自己的代碼(使用我建議你的),一旦完成,發佈你的答案,不接受我的答覆並接受你自己的答案。我想這個表格應該阻止了你。 – AndoAiron

0

第一:你不能實例化一個抽象類。你只能擴展一個抽象類。

第二:創建自己的WebViewClient類(MyWebViewClient)需要什麼。您可以在您的主要活動中使用webViewClient覆蓋shouldOverrideUrlLoading(WebView視圖,String url)方法,並在launchExternalBrowser(String url)方法中執行此方法。

我的意思是你正在觸發launchOternalBrowser(String url)方法裏面的意圖,它是從shouldOverrideUrlLoading(WebView視圖,String url)方法中調用的。那麼爲什麼你不應該在fireOverrideUrlLoading(WebView視圖,String url)方法裏面引發同樣的Intent方法。

+0

是的,謝謝。 –

+0

請幫助我http://stackoverflow.com/questions/27918108/progress-bar-not-working-webview-android –