WebView无法加载网页

时间:2017-10-22 05:18:39

标签: android android-webview

我正在尝试加载一个简单的PTI新页面。但是得到错误。以下是我的代码:

WebView newsContent = (WebView) findViewById(R.id.news_content);
    newsContent.getSettings().setJavaScriptEnabled(true);
    newsContent.getSettings().setLoadWithOverviewMode(true);
    newsContent.getSettings().setUseWideViewPort(true);
    newsContent.loadUrl("http://www.ptinews.com/news/9168439_Aadhaar-linkage-with-bank-accounts-mandatory--says-RBI.html");

错误是:

enter image description here

编辑:

我使用了以下内容:

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

1 个答案:

答案 0 :(得分:0)

您用于检查WebView

的设备

我已经实施了webview以及我的工作

<强> activity_webview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.dhruv.demoapiaitutorial.WebVIewActivity">

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

</RelativeLayout>

<强> WebViewActivity.java

package com.example.dhruv.demoapiaitutorial;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebVIewActivity extends AppCompatActivity {

    private WebView mWebview;
    private ProgressDialog dialog; //creating object of progress dialog

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        dialog = new ProgressDialog(this);
        dialog.setMessage("Please wait, your web is being open...");
        dialog.setCancelable(false);

        mWebview = (WebView) findViewById(R.id.webview);

        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                /*Showing progress dialog*/
                dialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                /*You can also access url in this overridden method*/
                /*closing progress dialog*/
                dialog.dismiss();
            }
        });


        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.getSettings().setLoadWithOverviewMode(true);
        mWebview.getSettings().setUseWideViewPort(true);
        mWebview.loadUrl("http://www.ptinews.com/news/9168439_Aadhaar-linkage-with-bank-accounts-mandatory--says-RBI.html");

    }

}
相关问题