这是我的AndroidManifest.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.webviewdemo" android:versionCode="1"
android:versionName="1.0.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".WebViewDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我正在编写的应用程序在 WebView 中显示了一个网页。该 URL 由用户输入,单击“开始”后,应显示该页面,但出现“网页不可用”错误。您可以在下面找到 Java 代码:
package org.example.webviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class WebViewDemo extends Activity {
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
private WebView webView;
private EditText urlField;
private Button goButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create reference to UI elements
webView = (WebView) findViewById(R.id.webview_compontent);
urlField = (EditText)findViewById(R.id.url);
goButton = (Button)findViewById(R.id.go_button);
// workaround so that the default browser doesn't take over
webView.setWebViewClient(new MyWebViewClient());
// Setup click listener
goButton.setOnClickListener( new OnClickListener() {
public void onClick(View view) {
openURL();
}
});
urlField.setOnKeyListener( new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_ENTER) {
openURL();
return true;
} else {
return false;
}
}
});
}
private void openURL() {
webView.loadUrl(urlField.getText().toString());
webView.requestFocus();
}
}