簡體   English   中英

WebView組件不顯示網頁:網頁不可用

[英]WebView component not show web page: Webpage not available

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上的結果如下:

在此處輸入圖片說明 在Android 6.0+頁面webUrl成功打開webUrl

如果我使用網址“ http://www.google.com ”,則說明打開成功。 另外,如果我在Web瀏覽器中打開webUrl ,則成功打開。

在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM