Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:52:40+00:00 2026-06-05T07:52:40+00:00

I am working on frame animation in which i am using thread but thread

  • 0

I am working on frame animation in which i am using thread but thread is throwing me error .
my code is as follow ….

public class Newton_LawsActivity extends Activity implements OnTouchListener, OnGestureListener,OnDoubleTapListener {
    /** Called when the activity is first created. */
    OnTouchListener l;
    ImageView animation;
    ImageView hideimage;
    TextView t;
    Thread timer;
    int Rid;
    double pixel;
    String law="null";
    private GestureDetector gestureDetector;
    float x,y;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gestureDetector = new GestureDetector(this);
        // the frame-by-frame animation defined as a xml file within the drawable folder
            animation = (ImageView)findViewById(R.id.image);
            //animation.setBackgroundResource(R.drawable.anatomy_5);
            hideimage=(ImageView)findViewById(R.id.imagein);
            t=(TextView) findViewById(R.id.t);
            animation.setBackgroundResource(R.anim.startanimation);
            final AnimationDrawable newtonAnimation = (AnimationDrawable) animation.getBackground();

           AnimationStart(newtonAnimation);

            animation.setOnTouchListener(this);      

        } 
    public void AnimationStart(final AnimationDrawable newanimation){
         timer=new Thread(){
        @Override
        public void run(){
                            try{

                                }catch(Exception e){}
                                finally{
                                            Newton_LawsActivity.this.runOnUiThread(new Runnable() {
                                                public void run(){
                                                newanimation.start();
                                            }});
                                        }
                            }
                        };
    timer.start();
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "animation", Toast.LENGTH_LONG).show();

        return gestureDetector.onTouchEvent(event);
    }


    private void animationswitch(float x, float y) {
        // TODO Auto-generated method stub
     pixel = getmaskpixel(x,y,animation,hideimage);
     Log.w("DEBUG","which is null:pixel" + pixel  );
     if (pixel==-583672){
         animation.setBackgroundResource(R.anim.firstlaw_01);
        final AnimationDrawable firstlaw_01 = (AnimationDrawable) animation.getBackground();
       law="firstlaw_02";
      firstlaw_01.start();
       animationstop_01();
     }
     t.setText(Double.toString(pixel));
    }


private double getmaskpixel(float x, float y, ImageView view,ImageView hideimage) {
        // TODO Auto-generated method stub
    Bitmap bm  = BitmapFactory.decodeResource(getResources(), R.drawable.touchnewtonfinal415);
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) view.getWidth()) / width;
    float scaleHeight = ((float) view.getHeight()) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);
    // matrix.postRotate(90);
    // recreate the new Bitmap
   Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height,matrix, false);
    Log.w("DEBUG","which is null:image " + hideimage.getWidth() + " OR " +hideimage.getHeight() );
    double bmWidth = view.getWidth();
      double bmHeight = view.getHeight(); 
      if ( x < 0 || y < 0 || x > hideimage.getWidth() || y >hideimage.getHeight()){
           return 0; //Invalid, return 0 
          }else{
           //Convert touched x, y on View to on Bitmap
           int xBm = (int)(x * (bmWidth / hideimage.getWidth()));
           int yBm = (int)(y * (bmHeight / hideimage.getHeight()));
           return bitmap.getPixel((int)xBm,(int) yBm); 
    }

}

    public boolean onSingleTapUp(MotionEvent e) {
        return false;
        // TODO Auto-generated method stub

    }
    @Override
    public boolean onDown(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public void onLongPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public void onShowPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        x=e.getX();
        y= e.getY();
        Log.w("DEBUG","which is null:image " + x + " OR " +y);
        animationswitch(x, y);
        return true;
    }

    void animationstop_01(){
                if(law=="firstlaw_02"){
                     timer=new Thread(){
                            @Override
                            public void run(){
                                                try{
                                                        Thread.sleep(8750);
                                                    }catch(Exception e){}
                                                    finally{
                                                        Log.w("debug","it is firstlaw_02");
                                                        animation.setBackgroundResource(R.anim.firstlaw_02);
                                                        final AnimationDrawable firstlaw_02 = (AnimationDrawable) animation.getBackground();
                                                        law="firstlaw_03";
                                                       AnimationStart(firstlaw_02);
                                                                }};


                                            };

                        timer.start();
                        }                                   

    }





    }

it is throwing me following error..

06-06 14:15:00.668: E/AndroidRuntime(966): FATAL EXCEPTION: Thread-10
06-06 14:15:00.668: E/AndroidRuntime(966): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-06 14:15:00.668: E/AndroidRuntime(966):  at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
06-06 14:15:00.668: E/AndroidRuntime(966):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
06-06 14:15:00.668: E/AndroidRuntime(966):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
06-06 14:15:00.668: E/AndroidRuntime(966):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
06-06 14:15:00.668: E/AndroidRuntime(966):  at android.view.View.invalidate(View.java:5139)
06-06 14:15:00.668: E/AndroidRuntime(966):  at android.view.View.setBackgroundDrawable(View.java:7486)
06-06 14:15:00.668: E/AndroidRuntime(966):  at android.view.View.setBackgroundResource(View.java:7395)
06-06 14:15:00.668: E/AndroidRuntime(966):  at Newtons.law.Newton_LawsActivity$2.run(Newton_LawsActivity.java:183)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:52:42+00:00Added an answer on June 5, 2026 at 7:52 am

    Change your animationstop_01 method to:

    void animationstop_01(){
    if(law=="firstlaw_02"){
         timer=new Thread(){
            @Override
            public void run(){
                    try{
                        Thread.sleep(8750);
                    }catch(Exception e){
                    }finally{
                        Log.w("debug","it is firstlaw_02");
                        Newton_LawsActivity.this.runOnUiThread(new Runnable() {
                            public void run(){
                                animation.setBackgroundResource(R.anim.firstlaw_02);
                                final AnimationDrawable firstlaw_02 = (AnimationDrawable) animation.getBackground();
                                law="firstlaw_03";
                                AnimationStart(firstlaw_02);
                             }
                         });
    
    
                     }
                 };
             };
             timer.start();
         }                                   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using frame animation for displaying some images. But it's working only in
The following key event is not working. Any idea? class Frame(wx.Frame): def __init__(self): wx.Frame.__init__(self,
I am using ajax animation extender in my application. It is working perfectly in
I have a animation class which stores the frames in a std::list, the header
I have a field which extends BitmapField (called AnimatedGIFField) and an AnimatorThread (extending Thread)
I have created animation.xml file in which i have added my stuff of frame
I'm working on a project which should have a progressdialog. But since i didn't
I'm trying to create a string randomiser with animation but its not working. I
I am working with a data frame where one of the columns consists of
I'm working on a webpage and I'm using frames. I'd like to make it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.