2
votes

So I have this AsyncTask that loads a WebView in onPostExecute that otherwise works perfectly, however I need to do something for onLoadResource so I'm trying to set a WebViewClient and for some reason that is causing a NullPointerException

private class newThread extends AsyncTask{
     private WebView webview;

...

protected void onPostExecute(Void result){
    setContentView(R.layout.reply);
    webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView  view, String  url){
            return true;
        }
        @Override
        public void onLoadResource(WebView  view, String  url){
            finish();
        }
    });
    webview = (WebView)findViewById(R.id.replyview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setScrollBarStyle(0);
    webview.loadDataWithBaseURL("http://example.com", "example", "text/html", "utf-8", null);
}

...


Is there something wrong in that code?

If I use basically the same method but outside of an AsyncTask then there's no errors. And if I remove the setWebViewClient lines then it runs fine.

1
Please post the stack trace or indicate more clearly where the NPE is occurring. - katsharp

1 Answers

1
votes

In your code, you try to call a function of webview (webview.setWebViewClient) before you assign anything to it (webview = (WebView)findViewById(R.id.replyview);) change the order of those two statements.