aperture
aperture

Reputation: 2895

Android - setting WebViewClient causes NullPointerException in AsyncTask

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.

Upvotes: 2

Views: 1940

Answers (1)

MByD
MByD

Reputation: 137432

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.

Upvotes: 3

Related Questions