3

I create a class and extend it from view. also implemented the Runnable interface. In the onTouchEvent i call: new Thread(this).start().
This is my class:

public class test extend View interface Runnable{

    ----some code------

    public void onTouchEvent(MotionEvent event){
        ----somecode------
        new thread(this).start();
        ----somecode------
    }

    public void run(){
        -----somecode-------
        invalidate();
        -----somecode-------
    }
}

but i receive this error:

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

How can i fix this?

M4N
  • 94,805
  • 45
  • 217
  • 260
Rasoul Taheri
  • 802
  • 3
  • 16
  • 32

6 Answers6

1

you need to update UI on UIThread, use

Activity activity = (Activity)getContext();
activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // update UI here
        invalidate();
    }
});
Berťák
  • 7,143
  • 2
  • 29
  • 38
  • but. runOnUiThread work on Activity. i'm in View(my class extended from View Class). – Rasoul Taheri Dec 17 '12 at 14:29
  • if you create your view by constructor, where you pass your activity as a context(`MyView myView = new MyView(MyActivity.this)`), you can get that activity by calling `Activity activity = (Activity)getContext();` in your view – Berťák Dec 17 '12 at 14:33
1

Try to use

postInvalidate()

instead of

invalidate()

ibit
  • 316
  • 1
  • 6
0

You can use handler class to make changes on your UI thread by sending messages.

http://developer.android.com/reference/android/os/Handler.html

Android, Handler messaging

Community
  • 1
  • 1
Damask
  • 1,754
  • 1
  • 13
  • 24
0

Since you are extending View class which binds to UI-thread, so anything you call in this class will run on the UI-thread. However, if you are using a new thread inside that class, then you may rely on its post(Runnable) method which will guarantee to run on the view's UI-thread. For example:

post(new Runnable() {

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

    }
});
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

this will run on UI thread from View class;

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

        }
    });
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
0

Try AsyncTask in android. It start new thread asynchronously and after completion of task it can update current UI without destroying current Activity.

Example Code :

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     // Task which should be completed in background.
     return null;
 }

 protected void onProgressUpdate(Integer... progress) {
     // Task which can be done while background process is progressing , such as updatng any UI element of Activity.
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     // Task to do after completion of asynctask.
     showDialog("Downloaded " + result + " bytes");
 }
}

You may also use runOnUiThread to do your task.

Both ways are very effective in updating UI in current activity.

Useful Link : http://developer.android.com/reference/android/os/AsyncTask.html http://sharecoding.wordpress.com/2012/05/22/simple-runonuithread-for-update-view-on-android/

Munish Thakur
  • 926
  • 1
  • 8
  • 25