Android webview显示白屏而不是在Android 4.1.2上加载网站.当我从网页浏览器打开它时,网站完全打开,我使用下面的代码
public class MainActivity extends AppCompatActivity { WebView web; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); web = (WebView) findViewById(R.id.webview); web.setWebViewClient(new myWebClient()); web.getSettings().setJavaScriptEnabled(true); web.loadUrl("www.Example.com"); } public class myWebClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub super.onPageStarted(view, url, favicon); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub view.loadUrl(url); return true; } } // To handle "Back" key press event for WebView to go back to previous screen. @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { web.goBack(); return true; } return super.onKeyDown(keyCode, event); }
}
记得设置协议"http:"
web.loadUrl("http://www.Example.com");
非常重要的是,请确保url在此时具有值:
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; }