Android: cargando la URL en la vista web desde la pantalla en blanco de la pantalla web por primera vez

I am trying to load multiple URL in webview which automatically change after some time interval, everything is working fine except that when i run my application first time every time it show's blank screen for first 10 second (Which is basically time period which i have set) and then it load's first URL, i don't know why it is happening like this.

A continuación se muestra mi código

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();
}
}

Thanks in advance. Any help would be appreciated...

preguntado el 27 de noviembre de 13 a las 07:11

5 Respuestas

You have not set any url in outside your MuCountDown Class that's why you are getting error. It means you have to give

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

atleast one URL inside your Gif_First class.

Use el código a continuación,

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

Respondido el 22 de Septiembre de 14 a las 12:09

syntax " CountDownTimer(start time, intervel time) " .. replace the code mTimer = new CountDownTimer(10000, 1000) en lugar de mTimer = new CountDownTimer(0, 1000) hope it will work. happy coding...

respondido 27 nov., 13:07

No, It is giving NullPointerException at mWebView.loadUrl(myArray[currentIndex]); on onFinish() method. - Asesino inocente

Use this custom :

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) {

}   
}

respondido 27 nov., 13:08

how to use this class, because i have implemented this in my code but no changes, same thing is happening, can you just give example how to use this in my class. - Asesino inocente

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) {

 }   
}
}

respondido 27 nov., 13:08

Ok, I did the same thing but still it is holding for 10 seconds. - Asesino inocente

chane first parameter to 0 - Macrosoft-Dev

If i do like this, mTimer = new CountDownTimer(0, 10000) it is showing error NullPointerException at mWebView.loadUrl(myArray[currentIndex]); on onFinish() method. - Asesino inocente

put check on that line: if (mArray[currentIndex]!=null) - Macrosoft-Dev

Nope, still not getting done, giving same error on same method. - Asesino inocente

There are possible reasons causing "WebView goes blank after finish loading page":

  1. If your WebView part defined in the layout xml file has dimensions like

    android: layout_width = "wrap_content"
    
    or

    android: layout_height = "wrap_content"
    

    Then after the page is loaded, the content is defined and WebView tries to "wrap" it, thus in some cases reduces the view's dimensions into 0dp and make your WebView invisible. Solution: change all those into fill_parent

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    
  2. This may be caused by an SSL certification error. Try this:

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

Respondido 21 Abr '15, 14:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.