0

这是我的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();
    }    
}
4

1 回答 1

0
private void openURL() {
    webView.loadUrl(urlField.getText().toString());
    webView.requestFocus();
} 

你能请 Log.d() urlField.getText().toString() 吗?只是为了确保 WebView 将使用什么字符串,如下所示:

Log.d("web string is ", urlField.getText().toString());

这样你就可以打开 LogCat 窗口并跟踪你的 WebView 的实际情况

于 2013-03-19T01:35:57.323 回答