java.lang.RuntimeException: An error occured while executing doInBackground()

后端 未结 4 609
[愿得一人]
[愿得一人] 2020-12-03 21:27

Sometimes i get this crash error in my app

java.lang.RuntimeException: An error occured while executing doInBackground()

This is the full l

相关标签:
4条回答
  • 2020-12-03 22:06

    You're showing a Toast in loadFeed which is called in the doInBackground part of your AsyncTask. You shouldn't access the UI from there.

    0 讨论(0)
  • 2020-12-03 22:19

    I think In this code Toast make problem for you,

    catch (Throwable t){
                  Log.e("OSFP.News",t.getMessage(),t);
    
                    Toast.makeText(nea.this, "Χρειάζεστε σύνδεση στο internet",
                            Toast.LENGTH_SHORT).show();
                    finish();
    
                }
    

    Because you are trying to do UI operation from worker thread of AsyncTask this will never allowed..

    UPDATE: If you want to update something from the doInBackGround() of AsyncTask use publishProgress(Progress...) and onProgressUpdate(Progress...).

    0 讨论(0)
  • 2020-12-03 22:20

    This is probably because you're trying to display something on doInBackground(). doInBackground runs in a worker thread which cannot do any UI work (including showing Toasts, which is what you're doing). Instead, all UI work should be done on onPostExecute().

    0 讨论(0)
  • 2020-12-03 22:25

    You can't do any GUI work on a background thread. Instead you should post a Runnable to a handler created in the GUI thread to make your Toast execute on the correct thread.

    EDIT: Since the homework tag was removed, I'll give an outline how to do it;

    • new() up a Handler in your GUI thread (onCreate is a good place) and save it in a member variable, say "handler".
    • Move showing your Toast to a Runnable (anonymous class is pretty good for this)
    • To show the toast, do a handler.post() with your Runnable as a parameter. The Runnable will execute in the GUI thread.

    For a complete example, see this article.

    0 讨论(0)
提交回复
热议问题