ERROR IN THE INBACKGROUND

Asked

Viewed 125 times

0

This function gives me the following error :

            @Override
            protected Boolean doInBackground(Void... args0) {
                updateJSONdata();
                return null;

            }

inserir a descrição da imagem aqui

  • 2

    Welcome to Stack Overflow! If you can better explain the problem you have in particular and, if possible, show code you have already done where this problem is, include the function code updateJSONdata();. Your question is too broad, see Help Center How To Ask..

2 answers

2

I believe inside updateJSONdata() you’re calling a Toast. This is not allowed because the Toast can only be called in the main thread, which is responsible for screen updates, while the method doInBackground() runs in a secondary thread, which is not allowed to update the screen.

This can also happen if you are instantiating a Handler inside updateJSONdata(). The Handler is able to execute code on the main thread and therefore call the Toast. If this is the case, one way to correct the error would be to instantiate the Handler passing by Looper.getMainLooper(), that is to say:

new Handler(Looper.getMainLooper())

But once you’re using AsyncTask, you shouldn’t instantiate Handler and rather execute instructions in the main thread using the AsyncTask, that is, overwriting and calling methods that own AsyncTask offers to update the screen during or after the execution of doInBackground().

These methods are:

  • publishProgress() to update the screen while running doInBackground() (in the case of publishProgress() you call via publishProgress() but the method to be overwritten is onProgressUpdate());

  • onPostExecute(result) or onCancelled(result) after the execution of doInBackground(). Unlike the latter, which runs on a separate thread, these methods I citei execute on the main thread (making use of a Handler internally) and are the appropriate methods for you to overwrite and update the screen.

I hope I’ve made it clear, any questions just comment.

0

Place the implementation of your function updateJSONdata(); to facilitate aid.

At first, within threads other than runOnUiThread you should not manipulate views from the main thread (Activity)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.