【问题标题】:Cannot display website using webview in android无法在android中使用webview显示网站
【发布时间】:2017-12-01 20:01:02
【问题描述】:

我正在 android studio 中使用 webview 制作一个简单的应用程序,但是当我运行它时,模拟器显示一个空白屏幕。

这是我的 activity_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.snehal.sample.MainActivity">

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

这是我的 AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snehal.sample">
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name="com.example.snehal.sample.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

这是我的 MainActivity.java 文件:

package com.example.snehal.sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity {

private WebView myWebView;

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

    myWebView = (WebView) findViewById(R.id.webview);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl("https://m.facebook.com");
}}

当我运行它时,它会在模拟器中给我这个空白屏幕。

我哪里出错了。请帮助我。 谢谢

【问题讨论】:

  • 我复制了你的代码并实现了它。它对我来说完全正常......一定有一些与网络相关的问题。我已经在 android 模拟器中测试了你的代码,而不是在真实设备中,它仍然对我有用..
  • 你的测试设备有网络连接吗?尝试在该设备上打开一些网站。

标签: java android webview


【解决方案1】:

试试这个

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

    myWebView = (WebView) findViewById(R.id.webview);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }
    });
    myWebView.loadUrl("https://m.facebook.com");                            
}

【讨论】:

  • 我在三星 a3 Android 7.0 和 genymotion 模拟器 Android 5.0 上进行了测试。在两台设备上都可以正常工作
猜你喜欢
  • 2019-03-24
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
相关资源
最近更新 更多