2011-11-18 83 views
2

我正尝试使用Android教程构建将Web视图加载到我构建的移动网站的应用程序。问题是跟着教程startActivity函数是未定义的,Android教程没有帮助。我做了Ctrl + Shift + O来验证所有正确的模块被加载。未定义类型为MyWebViewClient的方法startActivity(Intent)

package com.mysite; 

import android.content.Intent; 
import android.net.Uri; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class MyWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (Uri.parse(url).getHost().equals("www.mysite.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; 
    } 
} 

更新

好了,现在我的代码如下:

package com.myapp; 

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

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

     //init webview 
     WebView DCWebView = (WebView) findViewById(R.id.webview); 
     WebSettings webViewSettings = DCWebView.getSettings(); 

     //when a link is clicked, use the WebView instead of opening a new browser 
     DCWebView.setWebViewClient(new MyWebViewClient() { 
      @Override 
      public void launchExternalBrowser(String url) { 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
       startActivity(intent); 
      } 
     }); 

     //enable javascript 
     webViewSettings.setJavaScriptEnabled(true); 

    } 
} 

但我展示2个错误:

Description Resource Path Location Type 
The type new MyWebViewClient(){} must implement the inherited abstract method MyWebViewClient.launchExternalBrowser() DealClippings.java /MyApp/src/com/myapp line 21 Java Problem 

The method launchExternalBrowser(String) of type new MyWebViewClient(){} must override or implement a supertype method MyApp.java /DealClippings/src/com/myapp line 23 Java Problem 

回答

5

真的是因为没有startActivity方法WebViewClient。你可以检查docs。你必须发信号Context(可能是你的Activity)来代替执行这些代码行。在您的活动中设置WebViewWebViewClient时,有许多可能的方法,包括添加侦听器或只需调用您在此类的匿名实例中实现的抽象方法。

例如:

public abstract class MyWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (Uri.parse(url).getHost().equals("www.mysite.com")) { 
      // This is my web site, so do not override; let my WebView load the page 
      return false; 
     } 

     launchExternalBrowser(url);    
     return true; 
    } 

    public abstract void launchExternalBrowser(String url); 
} 

然后在你的活动:

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

虽然我不知道你为什么要这种行为完全是,但它应该工作或多或少。

+0

你是说我在Java中运行'startActivity()',它和在PHP中运行'$ this-> startActivity()'是一样的吗?我会试试...谢谢 – Webnet

+0

我不知道为什么你把PHP带了起来,但'startActivity'就是这个方法:http://developer.android.com/reference/android/content/Context.html#startActivity %28android.content.Intent%29 – kabuko

+0

我正在尝试学习Java ...在PHP中,你可以像'$ this-> methodName()那样在类中调用一个方法,并且我想知道'methodName()'是否等于'$ this-> methodName()',除了它是如何在Java中完成的 – Webnet

1

我不知道现在是否有很多问题回答你的编辑,18个月,但似乎这个问题有点流量,所以我会在这里发布这里的后代。

从您的错误中,听起来好像您还没有在MyWebViewClient的抽象类定义中为抽象方法提供参数。也就是说,你有这样的:

public abstract void launchExternalBrowser(); 

时,你应该有这样的:

public abstract void launchExternalBrowser(String url); 

错误的原因是Java治疗两种方法具有相同的名称但不同的参数作为两种截然不同的方法。所以launchExternalBrowser(String)是与launchExternalBrowser()不同的方法。

希望这可以帮助别人!

1

要在编辑之前回答原来的问题..

有同样的问题,并想出了MyWebViewClient,就是要在活动中的内部类。

package com.myapp; 

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

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

      //init webview 
      WebView DCWebView = (WebView) findViewById(R.id.webview); 
      WebSettings webViewSettings = DCWebView.getSettings(); 

      //when a link is clicked, use the WebView instead of opening a new browser 
      DCWebView.setWebViewClient(new MyWebViewClient()); 

      //enable javascript 
      webViewSettings.setJavaScriptEnabled(true); 

     } 

     private class MyWebViewClient extends WebViewClient { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       if (Uri.parse(url).getHost().equals("www.mysite.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; 
      } 
     } 
    } 
相关问题