Android:首次从网页显示空白屏幕在网页浏览中加载网址

时间:2013-11-27 07:42:27

标签: android timer webview

我正在尝试在webview中加载多个URL,这些URL会在一段时间间隔后自动更改,一切正常,但是当我第一次运行我的应用程序时,每次显示前10秒的空白屏幕时(这基本上是时间段,我已设置)然后它加载了第一个URL,我不知道为什么会这样发生。

以下是我的代码

public class Gif_First extends Activity {

WebView mWebView;
CountDownTimer mTimer;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.animation);

    mTimer = new CountDownTimer(10000, 1000) {
        private String[] myArray = {
                "http://i.share.pho.to/efe195fd_o.gif",
                "http://i.share.pho.to/cf478918_o.gif",
                "http://i.share.pho.to/5ae5a0a9_o.gif",
                "http://i.share.pho.to/17d0c96f_o.gif",
                "http://i.share.pho.to/d2139e2d_o.gif" };
        int currentIndex = 0;

        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            if (currentIndex < myArray.length) {
                mWebView.loadUrl(myArray[currentIndex]);
                currentIndex++;
            } else {
                currentIndex = 0;
                if (currentIndex < myArray.length)
                    mWebView.loadUrl(myArray[currentIndex]);
                currentIndex++;
                mTimer.start();
            }
            mTimer.start();
        }
    };

    mTimer.start();
    mWebView = (WebView) findViewById(R.id.webviewActionView);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new WebSliderWebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(mWebView, url);
            //Toast.makeText(getApplicationContext(), "Done!",
                //  Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            Toast.makeText(getApplicationContext(),
                    "Oh no! " + description, Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

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

public void onPause() {
    super.onPause();
    mTimer.cancel();
}
}

提前致谢。任何帮助将不胜感激......

5 个答案:

答案 0 :(得分:1)

您没有在MuCountDown类外部设置任何网址,这就是您收到错误的原因。这意味着你必须提供

mWebView.loadUrl("http://stackoverflow.com"); 

Gif_First类中至少有一个URL。

使用以下代码,

mTimer.start();
mWebView = (WebView) findViewById(R.id.webviewActionView);
mWebView.loadUrl("http://stackoverflow.com");
mWebView.getSettings().setJavaScriptEnabled(true);

答案 1 :(得分:0)

语法“CountDownTimer(start time, intervel time)”..替换代码mTimer = new CountDownTimer(10000, 1000)而不是mTimer = new CountDownTimer(0, 1000)希望它能够正常工作。快乐的编码...

答案 2 :(得分:0)

使用此自定义:

private class MyCountDown extends CountDownTimer
 {
long duration, interval;
public MyCountDown(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    // TODO Auto-generated constructor stub
    duration = millisInFuture;
    interval = countDownInterval;
    start();
}

@Override
public void onFinish() {

}

@Override
public void onTick(long duration) {

}   
}

答案 3 :(得分:0)

public class Gif_First extends Activity {

WebView mWebView;
CountDownTimer mTimer;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.animation);

mTimer = new MyCountDown(10000, 1000);
mWebView = (WebView) findViewById(R.id.webviewActionView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebSliderWebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(mWebView, url);
        //Toast.makeText(getApplicationContext(), "Done!",
            //  Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        Toast.makeText(getApplicationContext(),
                "Oh no! " + description, Toast.LENGTH_SHORT).show();
    }
});
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
    mWebView.goBack();
    return true;
}
return super.onKeyDown(keyCode, event);
 }

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

public void onPause() {
super.onPause();
mTimer.cancel();
}

class MyCountDown extends CountDownTimer
{
 long duration, interval;
 private String[] myArray = {
            "http://i.share.pho.to/efe195fd_o.gif",
            "http://i.share.pho.to/cf478918_o.gif",
            "http://i.share.pho.to/5ae5a0a9_o.gif",
            "http://i.share.pho.to/17d0c96f_o.gif",
            "http://i.share.pho.to/d2139e2d_o.gif" };
 int currentIndex = 0;
 public MyCountDown(long millisInFuture, long countDownInterval) {
  super(millisInFuture, countDownInterval);
  // TODO Auto-generated constructor stub
  duration = millisInFuture;
  interval = countDownInterval;
  start();
 }

 @Override
 public void onFinish() {
  if (currentIndex < myArray.length) {
            mWebView.loadUrl(myArray[currentIndex]);
            currentIndex++;
        } else {
            currentIndex = 0;
            if (currentIndex < myArray.length)
                mWebView.loadUrl(myArray[currentIndex]);
            currentIndex++; 
        }
 }

 @Override
 public void onTick(long duration) {

 }   
}
}

答案 4 :(得分:0)

可能的原因导致“完成加载页面后WebView变为空白”:

  1. 如果布局xml文件中定义的WebView部件的尺寸类似于

    android:layout_width="wrap_content"
    

    android:layout_height="wrap_content"
    

    然后在加载页面之后,定义内容并且WebView尝试“包装”它,因此在某些情况下将视图的尺寸缩小为0dp并使WebView不可见。 解决方案:将所有这些更改为fill_parent

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    
  2. 这可能是由SSL认证错误引起的。试试这个:

    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }