Anton Sobolev
Anton Sobolev

Reputation: 135

Why does Webview crash at first start?

In my Android app, when I create a new emulator and try to write in webview for the first time, it's not active. I can't write in textfield and then the app crashes. If I reload the app all works OK.

Code:

String url = "http://api.vkontakte.ru/oauth/authorize?client_id=2731649&scope=wall,notify,docs&" +
            "redirect_uri=http://api.vkontakte.ru/blank.html&display=wap&response_type=token";
    WebViewClass wvClforVK = new WebViewClass();

In oncreate:

webview= (WebView) findViewById(R.id.vkWebView);
         webview.setWebViewClient(wvClforVK);

On buttonclick:

webview.loadUrl(url);
in wvClforVK
 @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url); 
            return true;
        } 

Upvotes: 0

Views: 1433

Answers (2)

monx
monx

Reputation: 11

For android 4.2.2, Add loadUrl("about:blank") before you start loading your desired URL.

So,the final code is

WebView webView = (WebView)findViewById(R.id.my_web_view);
webview.loadUrl("about:blank");
webview.loadUrl("https://google.com");

Before adding loadUrl("about:blank"), when i run my app on android 4.2.2 crash everytime my webview try to load Url.

Upvotes: 1

Squonk
Squonk

Reputation: 48871

You must set the content view BEFORE attempting to use any components such as your R.id.vkWebView. If you use findViewById(...) before setting content view, it will return null.

To set the content view call...

setContentView(R.layout.main);

...assuming your layout file is called main.xml but you don't put the .xml part on it when using R.layout. Usually you will set the content view as early in an activity's onCreate(...) method as possible. This is often done immediately after the call to super.onCreate(...);

Upvotes: 1

Related Questions