-2
12-17 11:01:44.227: E/AndroidRuntime(1768): FATAL EXCEPTION: AsyncTask #1
12-17 11:01:44.227: E/AndroidRuntime(1768): java.lang.RuntimeException: An error occured while executing doInBackground()
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.lang.Thread.run(Thread.java:856)
12-17 11:01:44.227: E/AndroidRuntime(1768): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.view.View.requestLayout(View.java:15129)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.TextView.checkForRelayout(TextView.java:6303)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.TextView.setText(TextView.java:3547)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.TextView.setText(TextView.java:3405)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.widget.TextView.setText(TextView.java:3380)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at com.example.internet.MainActivity$RequestTask.doInBackground(MainActivity.java:150)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at com.example.internet.MainActivity$RequestTask.doInBackground(MainActivity.java:1)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-17 11:01:44.227: E/AndroidRuntime(1768):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-17 11:01:44.227: E/AndroidRuntime(1768):     ... 5 more
12-17 11:01:44.857: W/IInputConnectionWrapper(1768): showStatusIcon on inactive InputConnection
CRABOLO
  • 8,605
  • 39
  • 41
  • 68

2 Answers2

1

this is the cause.

Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

UI cant be updated from doInBackground

You should provide the code as well , specifically for the relevant part.

Here is an example of an approach that can be used

private class SaveProfile extends AsyncTask<String, Void, Boolean>{

@Override
protected Boolean doInBackground(String... params) {
    //---------- so your stuff here.... non ui related

    Log.v("response from saving",response);

    if(response.equals("1")){

        return true;                
    }else{

        return false;
    }
}

protected void onPostExecute(Boolean result) {

     if(result) {

                  //------ UPDATE UI HERE
         Toast.makeText(ProfileCompanyActivity.this, "profile saved", 2500).show();
     }else{
         Toast.makeText(ProfileCompanyActivity.this, "an error occured", 2500).show();
     }
 }
}
dmSherazi
  • 3,743
  • 5
  • 37
  • 62
0

First you must Understand the Concept of AsynTask in android...

1) you cannot update or change your UI element in backGround thread i.e doInBackGround()

2) use onPreExcecute() or onPostExcecute() to change or update your UI element

And to be Sure you are accessing UI element into doInBackGround

Check out this Example to Understant how the Methods work

Hope this could help....

Community
  • 1
  • 1
Nitesh Tiwari
  • 4,742
  • 3
  • 28
  • 46