23

I was using this, to DISPLAY IMAGES FROM THE INTERNET but it throws an error as below:
04-12 13:45:05.337: E/AndroidRuntime(27897): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

public class Order extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            new DownloadFilesTask().execute();       
        }    
        private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
            protected void onPostExecute(Void result) {
            }
             @Override
             protected Void doInBackground(Void... params) {
                 setContentView(R.layout.order);
                    ImageView imageView = (ImageView)findViewById(R.id.imgView);  
                    imageView.setImageDrawable(createDrawableFromURL("http://savagelook.com/misc/sl_drop2.png"));
                    return null;
             }          
        }     
        private Drawable createDrawableFromURL(String urlString) {
            Drawable image = null;
        try {
            URL url = new URL(urlString);
            InputStream is = (InputStream)url.getContent();
            image = Drawable.createFromStream(is, "src");
        } catch (MalformedURLException e) {
            image = null;
        } catch (IOException e) {
            image = null;
        } 
        return image;
        }

}
2

4 Answers 4

27

I got the same problem trying to change UI view from c++ using JNI. The solution was use

runOnUiThread(new Runnable() {
    public void run(){   
    }
});

runOnUiThread is an Activity method so I have to make my activity instance public static to be able to call on my public static method who later call from JNI.

Hope this help others :)

PS: from here I learn how to use JNI http://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_use_jni for my android game previously made with cocos2dx

1
20

Put this in onCreate()

ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
        imageView = (ImageView)findViewById(R.id.imgView);
        new DownloadFilesTask().execute();       
    } 

And your AsyncTask class should be like this,

        private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
             Drawable drawable;

             @Override
             protected Void doInBackground(Void... params) {
             drawable = createDrawableFromURL(
                                   "http://savagelook.com/misc/sl_drop2.png");
              return null;
             }
             protected void onPostExecute(Void result) {
                    imageView.setImageDrawable(drawable);
            }          
        } 
4
  • imageView in onPostExecute throw error. imageView cannot be resolved
    – chinna_82
    Apr 12, 2012 at 7:22
  • declare ImageView as global to Activity Apr 12, 2012 at 7:34
  • I think your supposed to be using a Thread Handler callback that the background thread calls with a message for the main UI thread to update the UI with (techotopia.com/index.php/…)
    – samus
    May 8, 2013 at 16:06
  • 4
    The most important point to note here is that view (for e.g. imageView) should be updated in onPostExecute method and not in doInBackground, which is the main cause of exception. doInBackground gets executed in different thread, while the onPostExecute gets executed in the caller thread which creates and executes an instance of subclass of AsyncTask.
    – Atharva
    Mar 26, 2014 at 11:29
3

I think this line is causing the error..

  imageView.setImageDrawable(createDrawableFromURL("http://savagelook.com/misc/sl_drop2.png"));

and the error explains why it is so..

     Only the original thread that created a view hierarchy can touch its views.

this error is caused because you are trying to change the User Interface on mainthread from some other thread.. here doInBackground in your case...

1
  • as Lalit Poptani told... his code solves.. and for imageView cannot be resolved.. declare ImageView imageView; like this before oncreate.. so that it is available for all functions..in the activity class..
    – 5hssba
    Apr 12, 2012 at 7:27
2

If you would like to do this job in fragment, you should call UI thread from activity in fragment.

getActivity().runOnUiThread(new Runnable() {
  public void run(){

    ... //your job 

  }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.