WebView组件不显示网页:网页不可用

时间:2018-09-28 14:51:41

标签: android android-webview

Android Studio 3.2。

在我的Manifiedt.xml中

<uses-permission android:name="android.permission.INTERNET" />

在我的xml布局中:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/webView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/aboutUsToolBar" />

    </android.support.constraint.ConstraintLayout>
</layout>

在我的活动中:

   private void init(String webUrl) {
        Debug.d(TAG, "init: load_url_in_web_view : " + webUrl);
        setSupportActionBar(findViewById(R.id.toolBar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        WebView webView = findViewById(R.id.webView);
        webView.setWebViewClient(new CustomWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        //webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.loadUrl(webUrl);
    }

    // open external web page inside WebView component
    private class CustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
            view.loadUrl(webUrl);
            return true;
        }
    }

在Android 4.4上显示以下结果:

enter image description here 在Android 6.0+页面webUrl上成功打开。

如果我使用网址“ http://www.google.com”,则说明成功。 另外,如果我在网络浏览器中打开webUrl,则会成功打开。

1 个答案:

答案 0 :(得分:0)

  

在AndroidManifest.xml中添加Internet权限。

 <uses-permission android:name="android.permission.INTERNET"/>
  

Main.xml

<WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  

Activity.java

private String postUrl = “https://www.google.com”;
 private WebView webView;

webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(postUrl);
webView.setHorizontalScrollBarEnabled(false);