线程抛出错误(帧动画)

时间:2012-06-06 08:48:55

标签: android multithreading

我正在处理框架动画,其中我正在使用线程,但线程正在抛出我的错误。 我的代码如下....

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();
                        }                                   

    }





    }

它让我跟着错误..

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)

3 个答案:

答案 0 :(得分:1)

错误位于您正在尝试的方法名称animationstop_01()内,

animation.setBackgroundResource(R.anim.firstlaw_02);

将其放在runOnUiThread()

答案 1 :(得分:1)

animationstop_01方法更改为:

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();
     }                                   
}

答案 2 :(得分:0)

试试这个:

animation.post(new Runnable() {

        public void run() {
            animation.setBackgroundResource(R.anim.firstlaw_02);

        }
    });

通过这种方式,您将其发布到UI线程。视图只能从创建视图的原始线程更改。