我正尝试使用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
你是说我在Java中运行'startActivity()',它和在PHP中运行'$ this-> startActivity()'是一样的吗?我会试试...谢谢 – Webnet
我不知道为什么你把PHP带了起来,但'startActivity'就是这个方法:http://developer.android.com/reference/android/content/Context.html#startActivity %28android.content.Intent%29 – kabuko
我正在尝试学习Java ...在PHP中,你可以像'$ this-> methodName()那样在类中调用一个方法,并且我想知道'methodName()'是否等于'$ this-> methodName()',除了它是如何在Java中完成的 – Webnet