【问题标题】:Getting White page Before my Webview loads in Android Webview App在我的 Webview 在 Android Webview App 中加载之前获取白页
【发布时间】:2016-08-30 19:46:25
【问题描述】:

我是 Android 开发的初学者,我面临的问题是我在加载 webview 之前得到白页。所以我想删除这个白页,以便在启动屏幕之后加载 webview 而不是白页,请帮我解决这个问题。

这是我的 Mainactivity.java 源代码:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import java.lang.reflect.Field;

@SuppressLint("SetJavaScriptEnabled")
@SuppressWarnings("ResourceAsColor")



public class MainActivity extends Activity {



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



        final SwipeRefreshLayout swipeView =(SwipeRefreshLayout)findViewById(R.id.container);
        ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
        NetworkInfo nInfo = cManager.getActiveNetworkInfo();

            //assign myWebView to webView
            final WebView myWebView = (WebView) findViewById(R.id.webView);


            //Load page URL
            myWebView.loadUrl("https://mobile-tech2dsk.blogspot.in");

            //Enable Javascript
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            //Link opens in the Webview

            myWebView.setWebViewClient(new WebViewClient(){






                public void onPageFinished(WebView view, String url) {
                    // do your stuff here

                    swipeView.setRefreshing(false);
                }



                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    super.onReceivedError(view, errorCode, description, failingUrl);
                    myWebView.loadUrl("file:///android_asset/error.html");

                }
            });



        swipeView.setColorScheme(android.R.color.holo_blue_dark,android.R.color.holo_blue_light);

        swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
        {



            @Override
            public void onRefresh()
            {
                swipeView.setRefreshing(true);

                ( new Handler()).postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        myWebView.loadUrl( "javascript:window.location.reload( true )" );

                    }
                }, 2000);
            }
        });

        }

    //Adding go back button for page history
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //assign myWebView to webView
        WebView myWebView = (WebView) findViewById(R.id.webView);

        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    }

这是我的 Activity_Main.xml 代码:

<android.support.v4.widget.SwipeRefreshLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity" >




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


</android.support.v4.widget.SwipeRefreshLayout>

【问题讨论】:

  • 请帮我解决这个问题
  • 您的页面需要时间来加载页面,这就是您看到白页的原因。
  • 如何添加进度条\
  • 分享您的 xml 布局。添加进度条请参考:stackoverflow.com/a/4331437/6127411
  • 这里是我的 activitymain.xml 源: schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" android:id="@ +id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >

标签: android webview


【解决方案1】:

看来,您的 webview 加载网页需要时间,所以您应该添加一个进度条。

您可以像这样添加进度条

在您的 Java 代码中

final ProgressDialog dialog = new ProgressDialog(MainActivity.this,R.style.MyProgressBar);
        dialog.setCancelable(false);
        dialog.setMessage("Loading Your Screen");
        dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        dialog.show();

myWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
            }

            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                //dialog.setMessage("Almost done...");
            }

            public void onPageFinished(WebView view, String url) {

                dialog.cancel();
            }

        });

// Load your Url
webView.loadUrl("Your Url");

如果需要,您可以添加样式

在您的 XML styles.xml 中

<style name="MyProgressBar" parent="android:Theme.Holo.Dialog">
        <item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:textColor">#FFFFFF</item>
        <item name="android:textStyle">normal</item>
        <item name="android:textSize">15sp</item>
    </style>

您的自定义对话框样式

<style name="CustomAlertDialogStyle">
        <item name="android:bottomBright">@color/blue</item>
        <item name="android:bottomDark">@color/blue</item>
        <item name="android:bottomMedium">@color/blue</item>
        <item name="android:centerBright">@color/blue</item>
        <item name="android:centerDark">@color/blue</item>
        <item name="android:centerMedium">@color/blue</item>
        <item name="android:fullBright">@color/blue</item>
        <item name="android:fullDark">@color/blue</item>
        <item name="android:topBright">@color/blue</item>
        <item name="android:topDark">@color/blue</item>
    </style>

在您的 colors.xml 中

<resources>
    <color name="transparent">#00000000</color>
    <color name="blue">#00695C</color>
</resources>

【讨论】:

  • 试过这个对话框在我的 webview 加载后也显示
  • 好的,看看我的编辑,我注释掉了 onPageStarted 消息。
  • 我使用了 Runnable progressRunnable = new Runnable() { @Override public void run() { dialog.cancel(); } };处理程序 pdCanceller = new Handler(); pdCanceller.postDelayed(progressRunnable, 4000);控制进度对话框
  • 无论如何感谢您的帮助:-)
  • 等等,这就像您刚刚在您的 postDelayed Runnable 代码中所做的“设置超时”!那么如果你的网速很慢,加载网页需要超过 4 秒呢???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多