如何在Android Studio中修复空白的Webview。

huangapple go评论82阅读模式
英文:

How to fix Blankwebview in Android studio

问题

我刚刚创建了一个简单的 WebView,在使用 URL 为 www.google.com 时正常工作,但当我使用自己的 URL 时,它不会显示在应用程序中,而是显示需要 JavaScript 来运行此应用程序。我已经启用了 JavaScript,但现在应用程序中什么也没有显示出来。以下是我的代码:

package com.------;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

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

        Browser = (WebView) findViewById(R.id.webview);
        Browser.setWebViewClient(new WebViewClient());
        Browser.loadUrl("-----");

        WebSettings webSettings1 = Browser.getSettings();
        webSettings1.setJavaScriptEnabled(true);

    }
}

以及清单文件(manifest):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.---------------">


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

以及错误信息:

38:36.242 10536-10536/? E/------: Unknown bits set in runtime_flags: 0x8000
2020-09-11 22:38:36.777 10536-10572/com---------- E/ion: ioctl c0044901 failed with code -1: Invalid argument
英文:

I just made a simple webview and it works perfectly fine when I use the URL as www.google.com but when I put my own URL it doesn't show up on the app instead it said you needed JavaScript to run this app. I enabled Java script and now there is nothing shown in the app.
Here's my code:

package com.------;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

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

        Browser = (WebView) findViewById(R.id.webview);
        Browser.setWebViewClient(new WebViewClient());
        Browser.loadUrl(&quot;-----&quot;);

        WebSettings webSettings1 = Browser.getSettings();
        webSettings1.setJavaScriptEnabled(true);

    }

and manifest:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.---------------&quot;&gt;


    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot;/&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot;/&gt;
    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

nd the error:

38:36.242 10536-10536/? E/------: Unknown bits set in runtime_flags: 0x8000
2020-09-11 22:38:36.777 10536-10572/com---------- E/ion: ioctl c0044901 failed with code -1: Invalid argument

答案1

得分: 2

你的代码看起来没有问题,而且你说过(当我使用网址www.google.com时,它可以正常工作),这说明问题出在你的网站上,而不是在WebView上。看起来你的网站需要使用JavaScript,我认为你需要使用浏览器的JavaScript引擎,而不是WebView引擎,比如Chrome的JavaScript引擎。

要使用Chrome的JavaScript引擎,你可以使用Chrome自定义标签(Chrome Custom Tabs)在你的应用中打开一个Chrome标签。Chrome自定义标签允许应用自定义Chrome的外观和感觉。应用可以更改诸如:

  • 工具栏颜色
  • 进入和退出动画
  • 向Chrome工具栏、溢出菜单和底部工具栏添加自定义操作

如果你的应用在自己的域之外的URL上引导用户,WebView是一个不错的解决方案。但如果是外部URL,我建议你使用Chrome自定义标签。你可以在https://github.com/GoogleChrome/custom-tabs-client找到一个完整的示例。它包含可重用的类来自定义用户界面,

英文:

your code is fine as i can see and you said that (it works perfectly fine when I use the URL as www.google.com) that tell you the problem is in your web site not in the web view it's look like your website need a java script and i think you have to use a browser java script engine not web view engine chrome java script engine as example

to use chrome java script engine you can use Chrome Custom Tabs to open an chrome tab on your app Chrome Custom Tabs allow an app to customize how Chrome looks and feels. An app can change things like:

Toolbar color Enter and exit animations Add custom actions to the Chrome toolbar, overflow menu and bottom toolbar The WebView is good solution if you are hosting your own content inside your app. If your app directs people to URLs outside your domain, i recommend that you use Chrome Custom Tabs A complete example is available at https://github.com/GoogleChrome/custom-tabs-client. It contains re-usable classes to customize the UI,

huangapple
  • 本文由 发表于 2020年9月12日 01:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63851487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定