1

Possible Duplicate:
Android : CalledFromWrongThreadException;: Only the original thread that created a view hierarchy can touch its views

Now I am working on an application.I want to show a progressbar until i get a true reply from called method.I used the following code.

 progressBar = new ProgressDialog(this);
      progressBar.setCancelable(true);
      progressBar.setMessage("Loading");
      progressBar.show();

new Thread(new Runnable() {


    @Override
    public void run() {
        // TODO Auto-generated method stub

        final boolean flag=  GetFixtureDetailsJsonFunction();

        if (flag==true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            progressBar.dismiss();
        }

    }
}).start();



protected boolean GetFixtureDetailsJsonFunction()
{ 
  //some code
  return true;
}

But I am getting the exception

03-14 15:48:23.722: E/AndroidRuntime(910): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Please help me friends

Community
  • 1
  • 1
sarath
  • 25
  • 1
  • 5

2 Answers2

0

change the way you are dismissing progressBar as below:

new Thread(new Runnable() {

@Override
public void run() {
    // TODO Auto-generated method stub

    final boolean flag=  GetFixtureDetailsJsonFunction();

    if (flag==true) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //dismissing prograssBar on UI thread
        Your_Activity_Class.this.runOnUiThread(new Runnable() {
           @Override
           public void run() {
              progressBar.dismiss();
           }
        });
    }

}
}).start();

actually you are dismissing prograssBar on a separate thread (non UI thread) which is wrong and thats why you are getting this exception. However, by using the code above, it dismisses the prograssBar on your UI thread.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • thanks for the reply...so instead of new Thread(new Runnable() { } ..i have to use Activity_Class.this.runOnUiThread(new Runnable() { } or i have to create Activity_Class.this.runOnUiThread(new Runnable() { } inside the new Thread(new Runnable() { } block to dismiss the progressbar...which i have to do? – sarath Mar 14 '12 at 10:55
  • yes, you need to perform it inside the thread. Check my updated answer – waqaslam Mar 14 '12 at 10:58
  • then also I am getting the same exception – sarath Mar 14 '12 at 12:04
  • update your question by pasting your entire activity class including my suggestion – waqaslam Mar 14 '12 at 12:06
0

your problem is that one of your thread is trying to access the data that belongs to another thread, you cannot do that because that can lead to serious data corruption.

follow the following article for solution to the problem.

https://web.archive.org/web/20200810154212/http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Mayank
  • 8,777
  • 4
  • 35
  • 60