【问题标题】:White blank screen using webview使用 webview 的白色空白屏幕
【发布时间】:2019-04-19 16:13:11
【问题描述】:

我正在尝试使用 webview 布局启动活动,而不打开新的 web 浏览器。所以它会被嵌入到应用程序中。

似乎它工作了几天,现在......我只是得到一个白色的空白屏幕。你能帮我一下吗?

这是我的 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"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>


<Button
    android:id="@+id/backBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:height="50dip"
    android:background="#4d867f"
    android:text="Back"
    android:textColor="#f9f9f9"
    android:textSize="20dip" />

</LinearLayout>
</RelativeLayout>

我是这样称呼这个活动的:

promotionBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, PromotionActivity.class);
            intent.putExtra("url", "https://www.test.com/apppromotion.html");
            startActivity(intent);
        }
    });

最后是活动:

package br.com.test.androidtools;

import android.content.Intent;
import android.net.Uri;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
import android.webkit.WebViewClient;




public class PromotionActivity extends Activity {

private WebView webView;
private Button backBtn;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        Intent it = new Intent(PromotionActivity.this, MainActivity.class);
        startActivity((Intent)it);

        return true;

    }

    return super.onKeyDown(keyCode, event);
}

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

    backBtn = (Button) findViewById(R.id.backBtn);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);

    Bundle bundle = getIntent().getExtras();
    webView.loadUrl(bundle.getString("url"));

    webView.setWebViewClient(new myWebViewClient());

    backBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent it = new Intent(PromotionActivity.this, MainActivity.class);
            startActivity((Intent)it);
        }
    });



}




public class myWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

}

有什么办法可以解决这个问题吗? 谢谢!

【问题讨论】:

  • 调试这个,在 webView.loadUrl(bundle.getString("url")); 之前,使用 Log.d("test", "url = " + bundle.getString("网址"));那里设置正确吗?此外,请检查您的日志以了解您的 WebView 可能打印的任何警告。
  • 嗨弗兰克!它正在获取正确的网址...
  • 但我也收到此错误:“E/chromium: [ERROR:buffer_manager.cc(313)] [.Parent-Compositor-0xe2f17b00]GL ERROR :GL_INVALID_OPERATION : glBufferData:

标签: android webview


【解决方案1】:

url 目标的 SSL“Lets Encrypt”配置不正确。 如此简单,但我花了一段时间才发现!因此,在寻找难以破解的解决方案之前,只需将 webviewer 指向另一个 url。

【讨论】:

    【解决方案2】:

    改变

    Bundle bundle = getIntent().getExtras();
    webView.loadUrl(bundle.getString("url"));
    

    收件人

    webView.loadUrl(getIntent().getStringExtra("url"));
    

    注意

    如果你使用Bundle,下一个Activity可以使用Bundle bundle = getIntent().getExtras();

    编辑

    • 在清单中添加权限。

      <uses-permission android:name="android.permission.INTERNET" />
      
    • 添加设置

      webView.getSettings().setJavaScriptEnabled(true);
      webView.getSettings().setSupportZoom(false);
      webView.getSettings().setJavaScriptEnabled(true);
      
    • 确保https://www.test.com/apppromotion.html可以正常加载。

    编辑

    确保后退按钮可以显示在屏幕上。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:orientation="vertical">
    
            <WebView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/webView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" />
    
    
            <Button
                android:id="@+id/backBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:height="50dip"
                android:background="#4d867f"
                android:text="Back"
                android:textColor="#f9f9f9"
                android:textSize="20dip" />
    
        </LinearLayout>
    </RelativeLayout>
    

    编辑

    你可以试试这个。

    <WebView 
        android:id="@+id/web_view" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layerType="hardware"/>
    

    你可以在xml代码中使用android:layerType

    【讨论】:

    • 你好,可六月!试过了,但似乎我必须改变一些额外的东西......对吧?它没有用......我也尝试了下一个代码但仍然相同:
    • 字符串 url = getIntent().getStringExtra("url"); webView.loadUrl(url);
    猜你喜欢
    • 1970-01-01
    • 2014-01-13
    • 2020-07-17
    • 2013-08-25
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多