java - 无法在 webview 中加载 url - 黑屏

标签 java android url android-webview

我一直在尝试几种方法,其中一些在这里找到,在我的应用程序中使用 webview 加载 url,但到目前为止,对于来自客户端的一个特定页面,所有这些方法都没有成功。让我们以这种方法为例:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/background">
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="@drawable/top_heading"
        android:id="@+id/rlayout1">
        <TextView android:layout_width="wrap_content"
            android:layout_centerVertical="true" android:layout_centerHorizontal="true"
            android:textColor="#ffffff" android:textSize="22dip"
            android:textStyle="bold" android:layout_height="wrap_content"
            android:text="More Information" android:id="@+id/txtviewfbdisplaytitle" />
    </RelativeLayout>
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_below="@+id/rlayout1"
        android:id="@+id/rlayout2">
        <WebView android:id="@+id/webview1" android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0" />
    </RelativeLayout>
</RelativeLayout>

MainActivity.Java

public class MainActivity extends Activity {
    private class MyWebViewClient extends WebViewClient {
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
              view.loadUrl(url);
              return true;
          }
    }
    Button btnBack;
    WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        webview=(WebView)findViewById(R.id.webview1);
        webview.setWebViewClient(new MyWebViewClient());
        openURL();
    }

     /** Opens the URL in a browser */
    private void openURL() {
        webview.loadUrl("[see solution]");
        webview.requestFocus();
    }
}

虽然其他网站可以工作,但这个网站却不行。我对 Android 编程相当陌生。接下来我可以尝试什么?

附带问题:URL 有什么不同的作用来阻止成功加载?

最佳答案

该网站使用 DOM 存储来运行,并且由于默认情况下 DOM 存储在 android webview 中处于禁用状态,因此您必须启用它。

webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
openURL();

关于java - 无法在 webview 中加载 url - 黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54097683/

相关文章:

java - 从 CrudRepository Spring Boot 访问嵌入式类的字段

Android - 如何知道设备是否只有 WiFi?

android - 是否有可能以描述取决于浏览器/Android 语言的方式在 google play 中发布应用程序?

database - 使用 slug 而不是 ID 获取记录

xml - 如何下载 URL 存储在 xml 文档中的多个文件?

java - Glassfish 崩溃(停止),没有调试信息

java - 在不同的类中返回更新后的值时遇到问题

java - 如何创建自定义 GWT CellTable 类?

java - 离开 parent 时Android View 消失

html - 固定页眉与页内 anchor 重叠