android.view.ViewRoot$CalledFromWrongThreadException:

后端 未结 3 1864
灰色年华
灰色年华 2021-01-07 12:48

I\'m getting an error in my logcat stating:

java.net.UnknownHostException: apps.example.com
09-13 14:57:28.970: W/System.err(3823): android.view.ViewRoot$Cal         


        
相关标签:
3条回答
  • 2021-01-07 13:33

    You are trying to update an UI element from a Thread that is not the UI Thread.

    0 讨论(0)
  • 2021-01-07 13:36
     android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    

    Looks like you are trying to update the UI on a background Thread like in doInBackground() of your AsyncTask. You can't do this. You need to update the UI on a UI Thread. Use any of the other AsyncTask methods to do this such as

    onPreExecute()

    onProgressUpdate()

    or

    onPostExecute()

    AsyncTask Docs

    you may also want to see

    Processes and Threads

    0 讨论(0)
  • 2021-01-07 13:40

    In

           protected InputStream doInBackground(String... params)
    

    You have

          stream = getQueryResults(Base_URL);
    

    Then In getQueryResults you have

        catch (Exception e) {
                Log.e(LOG_TAG, e.toString());
                tryagain();
    
            }
    

    The In tryagain you have

        public void tryagain() {
        setContentView(R.layout.tryagain);
    

    You cannot update/access ui from a background thread. That is why you are getting that exception.

    Also you should re consider your design. If you have setContentView more than once in the same activity re consider your design.

    Also instead of mutiple try blocks have one try block and multiple catch blocks.

    Not sure what you want but you are calling publishProgress more than once.

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