Landei00
Landei00

Reputation: 51

Problems with WebViewClient and onKeyDown

I'm currently trying to make a WebView based app.

This is the code I currently have:

    WebView webview = new WebView(this);
    setContentView(webview);

    WebView webView = (WebView) findViewById(R.id.myWebView);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadsImagesAutomatically(true);

    webView.setWebViewClient(new NewWebViewClient());
    webview.loadUrl("http://google.com");

And the "NewWebViewClient" class is under this also in the MainActivity.java

public class NewWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;
    }
}

There is no error shown by Android Studio. But if I want to start the app on my smartphone it just crashes.

The same Problem I have with this:

public void onKeyDown() {
    WebView webView = (WebView) findViewById(R.id.myWebView);

    if (webView.copyBackForwardList().getCurrentIndex() > 0) {
        webView.goBack();
    }

    else {
        super.onBackPressed();
    }

}

Does anyone know how I get this both things get working?

EDIT:

It still crashes:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference

Upvotes: 0

Views: 1646

Answers (2)

Tomas Antos
Tomas Antos

Reputation: 284

I would omit this statement:

WebView webview = new WebView(this);

You don't need to construct WebView like that, onCreate() event in the browser's activity class will take care of initiating the object. Then, instead of calling setContentView() referring to the webview you constructed:

setContentView(webview);

Refer to the layout that contains the WebView, for example:

setContentView(R.layout.browser);

Then you can refer to the WebView via its id. I think problem is that findViewById() is not finding the WebView by its ID, because you haven't set the layout that contains element with that ID before. That's achieved via setContentView(). FindViewById() method needs to know in which view to look for an element with particular ID, for instance, sometimes you need to inflate the view first (alternative to the setContentView):

View  v =  LayoutInflater.from(getApplicationContext()).inflate(R.layout.some_layout, null);

then you call findViewById against this view to find an element within layout:

v.findViewById(R.id.element_id);

So, use findViewById() without view prefix if you set the view via setContentView(), pointing to the layout first, and use view.findViewById() syntax if you inflated the view via LayoutInflater, without the use of setContentView().

Upvotes: 0

Rishad Appat
Rishad Appat

Reputation: 1806

Try this

 @Override
public void onBackPressed() {
        WebView webView = (WebView) findViewById(R.id.myWebView);
    if(webView!=null)
               {
                  if(webView.canGoBack())
                    {
                        webView.goBack();
                    }
            else
                {
                  super.onBackPressed();
                }
           }
   }

After reading ur question, i think u need to perform go back when the user press the back button... If thatz whats u luking for u can use the above code...

And replace

 WebView webview = new WebView(this);

with

WebView webView = (WebView) findViewById(R.id.myWebView);

Upvotes: 1

Related Questions