title: Android using progress bar in a fragment
body: <p>I have been trying to make a fragment that can retrieve an image when is loaded with an progress bar. But when the fragment calls doWork() it makes the app crashes. The import of the image is note the problem.
  I tried to use simple stuff like just display a Toast and crashes too.</p>
  
  <p>(This works perfectly in an activity but not in the fragment)</p>
  
  <p>Here is my code:</p>
  
  <p>Main Activity:</p>
  
  <pre><code>    public class MainActivity extends FragmentActivity {
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
      }
  
      public void setLayout(View view){
          PictureLoader pictureLoader = new PictureLoader();  
          FragmentManager fm = getSupportFragmentManager();
          FragmentTransaction transaction = fm.beginTransaction();
          transaction.replace(R.id.fragmentContainer, pictureLoader);
          transaction.commit();   
      }
  }
  </code></pre>
  
  <p>Fragment:</p>
  
  <pre><code>public class PictureLoader extends Fragment{
      ImageView iv;
      ProgressBar progress;
      int progressStatus;
      Handler mHandler;
      int progressValue;
  
      @Override
      public View onCreateView(LayoutInflater inflater,
          @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.photo_loader,container, false);
  
      progress = (ProgressBar)view.findViewById(R.id.progressBar1);
      mHandler = new Handler();
       progress.setVisibility(View.GONE);
  
      iv = (ImageView)view.findViewById(R.id.imageView1);
          loadImage();
      return view;
  
      }
  
  public void loadImage(){
  
      progress.setVisibility(View.VISIBLE);
       new Thread(new Runnable() {
           public void run() {
  
  
               while (progressStatus &lt; 100) {
                   progressStatus = doWork();
  
                   // Update the progress bar
                   mHandler.post(new Runnable() {
                       public void run() {
                          progress.setProgress(progressStatus);
                       }
                   });
               }
  
               if (progressStatus &gt;= 100) {
                   progress.setVisibility(View.GONE);
  
               }
           }
  
          private int doWork() {
  
                  File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                  File outputDir = new File(picturesDir, "All pictures");
                  Bitmap bitmap = BitmapFactory.decodeFile(outputDir.getPath() + "/" + "IMG_201407211_183804.jpg");
                  iv.setImageBitmap(bitmap);
              Toast.makeText(getActivity(), "Done", Toast.LENGTH_LONG).show();
                  return 100;
          }
       }).start();
  
  
  }
  
  
  }
  </code></pre>
  
  <p>Log: </p>
  
  <pre><code>08-09 13:35:16.172: D/(4138): HostConnection::get() New Host Connection established 0xb9223bc0, tid 4138
  08-09 13:35:16.172: D/libEGL(4138): loaded /system/lib/egl/libGLESv1_CM_emulation.so
  08-09 13:35:16.192: D/libEGL(4138): loaded /system/lib/egl/libGLESv2_emulation.so
  08-09 13:35:16.302: W/EGL_emulation(4138): eglSurfaceAttrib not implemented
  08-09 13:35:16.362: D/OpenGLRenderer(4138): Enabling debug mode 0
  08-09 13:35:20.402: D/dalvikvm(4138): GC_FOR_ALLOC freed 65K, 10% free 2852K/3140K, paused 68ms, total 71ms
  08-09 13:35:20.512: D/dalvikvm(4138): GC_FOR_ALLOC freed 9K, 10% free 2975K/3272K, paused 48ms, total 49ms
  08-09 13:35:20.522: I/dalvikvm-heap(4138): Grow heap (frag case) to 4.289MB for 1228812-byte allocation
  08-09 13:35:20.592: D/dalvikvm(4138): GC_FOR_ALLOC freed &lt;1K, 7% free 4174K/4476K, paused 65ms, total 65ms
  08-09 13:35:20.722: W/dalvikvm(4138): threadid=11: thread exiting with uncaught exception (group=0xb0eaf648)
  08-09 13:35:20.732: E/AndroidRuntime(4138): FATAL EXCEPTION: Thread-219
  08-09 13:35:20.732: E/AndroidRuntime(4138): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.View.requestLayout(View.java:15792)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.View.requestLayout(View.java:15792)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.View.requestLayout(View.java:15792)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.View.requestLayout(View.java:15792)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.View.requestLayout(View.java:15792)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.View.requestLayout(View.java:15792)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.View.requestLayout(View.java:15792)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.view.View.requestLayout(View.java:15792)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.widget.ImageView.setImageDrawable(ImageView.java:422)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at android.widget.ImageView.setImageBitmap(ImageView.java:437)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at com.example.photoloader.PictureLoader$1.doWork(PictureLoader.java:70)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at com.example.photoloader.PictureLoader$1.run(PictureLoader.java:49)
  08-09 13:35:20.732: E/AndroidRuntime(4138):     at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p>I hope You can help me. Thank you</p>