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.