开发者

"Hello, WebView" example

I'm new in android development and I am trying out the WebView example in the official android site.

http://developer.android.com/guide/tutorials/views/hello-webview.html

But I do everything they say...which is pretty simple: I create the project, edit the layout file, then I add the code, etc. No problems building...but when I launch the app in the simulator I 开发者_如何学运维just got a black screen. It is like if the Layout is empty...like if the WebView is not created.

What am I doing wrong?


Sorry about that – that link is a bit outdated. The fixed version of this tutorial is available here:

http://developer.android.com/guide/webapps/webview.html

We should remove the old link; I'll file a bug.

And note, the error is that setContentView isn't being called.


in oncreate method add WebView.enablePlatformNotifications();

in manifest file add

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

now it works fine...


    webview_id = (WebView)findViewById(R.id.webview_id);
    webview_id.getSettings().setJavaScriptEnabled(true); // enable javascript
    WebSettings webSettings = webview_id.getSettings();
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(true);
    webSettings.setPluginState(WebSettings.PluginState.ON);
    webSettings.setJavaScriptEnabled(true);
    webview_id.setInitialScale(90);
    webSettings.setLoadWithOverviewMode(true);
    webview_id.requestFocusFromTouch();
    webview_id.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Snackbar.with(getApplicationContext()).dismiss();
            Snackbar.with(getApplicationContext()) // context
                    .text(description) // text to display
                    .show(MainActivity.this);
        }
        public void onPageFinished(WebView view, String url) {
            progressBar.setVisibility(View.GONE);
        }
    });


    if(isNetworkAvailable()){

        webview_id .loadUrl("http://helloworld.org/");
    }else{
        Snackbar.with(getApplicationContext()).dismiss();
        Snackbar.with(getApplicationContext()) // context
                .text("Please Check your Internet Connection") // text to display
                .show(MainActivity.this);
        progressBar.setVisibility(View.VISIBLE);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜