1

I am trying to display a progress bar in my application but am running into a problem with threading. Here is the code I am using to do it:

package com.integrated.mpr;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Progess extends Activity {

   static String[] display = new String[Choose.n];
   private static final int Progress = 0;

   ProgressBar bar;
   TextView label;
   Handler handler = new Handler();

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.progress);

      bar = (ProgressBar) findViewById(R.id.progBar);

      new Thread(new Runnable() {

         int i = 0;
         int progressStatus = 0;

         public void run() {
            while (progressStatus < 100) {
               progressStatus += doWork();
               try {
                  Thread.sleep(500);
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }

               // Update the progress bar
               handler.post(new Runnable() {
                  public void run() {
                     bar.setProgress(progressStatus);
                     i++;
                  }
               });
            }
         }

         private int doWork() {
            display = new Logic().finaldata();
            return i * 3;
         }

      }).start();

      Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST");
      startActivity(openList);
   }
}

I am getting the following logcat message:

05-30 12:38:00.082: E/AndroidRuntime(17332): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Please help me, I am new to threading.

Perception
  • 79,279
  • 19
  • 185
  • 195
Kumar
  • 169
  • 2
  • 5
  • 12

3 Answers3

4

Instead of implementing thread this way, i would suggest you to implement AsyncTask which is known as Painless Threading in Android.

  1. display progress bar inside onPreExecute()
  2. do background tasks inside the doInBackground()
  3. dismiss progress bar inside onPostExecute()
  4. update progress bar inside onProgressUpdate()
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • I also tried to use AsyncTask but it was giving an error in the doinbackground() .. trying to modify the UI like that – Kumar May 30 '12 at 07:30
  • @Kumar then post the error/exception of doInBackground() here so can help you. – Paresh Mayani May 30 '12 at 07:34
  • 1
    Because you shouldn't update UI in `doInBackground()`. Use `onProgressUpdate()` and `onPostxecute()` for that. Or use AsyncTaskLoader which is even better, because you don't have to care for the context leaks (use AsyncTask if you know how to control its lifecycle according to the contexts lifecycle) – Michał Klimczak May 30 '12 at 07:34
  • I would recommend this solutions.. and you should not update UI from doInBackground but from onPostExecute.. Just for records in in onProgressUpdate (between doInBackground and onPostExecute) you should update progress bar and you can update UI with temporal/current results if you need so. Hope it helps – Ewoks May 30 '12 at 07:36
  • @Ewoks yes you are 80% right, but you can even update UI from doInBackground() by using **`runOnUiThread`**. – Paresh Mayani May 30 '12 at 07:37
  • sorry @PareshMayani I find this a bit missleading.. Never thought combination of `doInBackground` and `runOnUiThread` might be reasonable or useful. If I have some expensive work I want to move it **from** UI thread, that's why I would use `doInBackground`. In that case I don't see the point of `runOnUiThread` .. maybe I am just wrong :S p.s. is it possible that u got my previous answer wrong? I was recommending your proposal, no? – Ewoks May 30 '12 at 08:22
  • @Ewoks never mind...i was trying to make light on your comment :) – Paresh Mayani May 30 '12 at 08:44
0

I think answer is in the error message from your question - just write Looper.prepare() before handler.post(new Runnable() ...).

Here are some references:

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

For a quick one, try the code below.

runOnUiThread(new Runnable(){
    @Override
    public void run() {
        bar.setProgress(progressStatus);
        i++;

    }  
}
Perception
  • 79,279
  • 19
  • 185
  • 195
Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52