繁体   English   中英

使用webview的白色空白屏幕

[英]White blank screen using webview

我正在尝试通过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;
    }
}

}

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

更改

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目标的SSL“ Lets Encrypt”配置错误。 很简单,但是花了我一段时间才发现! 因此,在寻找难以破解的解决方案之前,只需将webviewer指向另一个URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM