如何修复仅显示链接和Android徽标的Android WebView

时间:2014-01-28 08:54:58

标签: android webview

在Android应用程序中加载WebView时,它只显示该页面的链接,而不是黑色背景上的android徽标旁边的网站内容。重新启动应用程序后,内容已加载。

Java代码:

public class MainActivity extends Activity {

    private WebView webView;
    private String baseUrl = "http://www.example.com";

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

        webView = (WebView) findViewById(R.id.web_view);
        webView.setId(10001);
        webView.setBackgroundColor(0xFF000000);
        // webView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);

        final Activity activity = this;
        final ProgressDialog progressDialog = new ProgressDialog(activity);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().setGravity(Gravity.BOTTOM);

        hideProgressbarText(progressDialog);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressDialog.show();
                progressDialog.setProgress(0);
                activity.setProgress(progress * 1000);
                progressDialog.incrementProgressBy(progress);
                if (progress == 100 && progressDialog.isShowing())
                    progressDialog.dismiss();
            }
        });

        webView.setWebViewClient(new WebViewClient());

        // final ProgressBar progressBar= (ProgressBar)
        // findViewById(R.id.progressbar);

        WebSettings settings = webView.getSettings();
        if (settings != null) {
            settings.setJavaScriptEnabled(true);
            settings.setDomStorageEnabled(true);
            settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        }

        if (savedInstanceState == null) {
            webView.loadUrl(baseUrl);
        }

    }

    public void onBackPressed() {

        if (webView.isFocused() && webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
            finish();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void hideProgressbarText(ProgressDialog progressDialog) {
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            progressDialog.setProgressNumberFormat(null);
            progressDialog.setProgressPercentFormat(null);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        webView.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        webView.restoreState(savedInstanceState);
    }

}

截图:

enter image description here

2 个答案:

答案 0 :(得分:1)

似乎你所拥有的是错误页面,例如page_not_foundDNS_failure。即你看到的是错误消息或提示文本,它应该是BLACK颜色。但是,您只需将背景颜色设置为0xFF000000(与文本颜色相同),这将隐藏提示文本。

删除black背景,您可能会看到该文字。我想现在显示的不是你的页面内容,而是来自webkit引擎的一些错误提示。

答案 1 :(得分:0)

使用本网站webview参考,他们解释了良好的方式

请确保

<uses-permission android:name="android.permission.INTERNET" />