title: Fatal exception during runtime in the application in Android?
body: <p>I am developing an small application in Android in which I have to show images. I have 2 images in my application but i have to make functionality to move the images by finger touch therefore i use the viewflipper in my application and I have also want zoomin and zoomout functionality.</p>
  
  <p>I had also make 3 classes for xoom in and out.But while I am running the application it will show the first image and also zoom it when itouch and flip it it will move to second image after that i flip it back and try to zoom it my application will crash and give the below message in the logcat</p>
  
  <pre><code>06-17 15:22:17.003: ERROR/AndroidRuntime(6770): FATAL EXCEPTION: main 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770): java.lang.ClassCastException: android.widget.LinearLayout 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at org.example.touch.Touch.onTouch(Touch.java:117) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.view.View.dispatchTouchEvent(View.java:3762) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.os.Handler.dispatchMessage(Handler.java:99) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.os.Looper.loop(Looper.java:123) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at android.app.ActivityThread.main(ActivityThread.java:4627) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at java.lang.reflect.Method.invokeNative(Native Method) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at java.lang.reflect.Method.invoke(Method.java:521) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
  06-17 15:22:17.003: ERROR/AndroidRuntime(6770):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>here is my java code</p>
  
  <pre><code>/***
   * Excerpted from "Hello, Android! 3e",
   * published by The Pragmatic Bookshelf.
   * Copyrights apply to this code. It may not be used to create training material, 
   * courses, books, articles, and the like. Contact us if you are in doubt.
   * We make no guarantees that this code is fit for any purpose. 
   * Visit http://www.pragmaticprogrammer.com/titles/eband3 for more book information.
  ***/
  package org.example.touch;
  
  
  
  import android.app.Activity;
  import android.graphics.Matrix;
  import android.graphics.PointF;
  import android.os.Bundle;
  import android.util.FloatMath;
  import android.util.Log;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.View.OnTouchListener;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.ViewFlipper;
  
  public class Touch extends Activity implements OnTouchListener {
     private static final String TAG = "Touch";
     // These matrices will be used to move and zoom image
     Matrix matrix = new Matrix();
     Matrix savedMatrix = new Matrix();
  
     // We can be in one of these 3 states
     static final int NONE = 0;
     static final int DRAG = 1;
     static final int ZOOM = 2;
     int mode = NONE;
    float downXValue;
    int counter = 0;
     // Remember some things for zooming
     PointF start = new PointF();
     PointF mid = new PointF();
     float oldDist = 1f;
  
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView view = (ImageView) findViewById(R.id.imageView);
        ImageView view1 = (ImageView) findViewById(R.id.imageView1);
        view.setOnTouchListener(this);
        LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
        layMain.setOnTouchListener((OnTouchListener) this); 
  
        // ...
        // Work around a Cupcake bug
        matrix.setTranslate(1f, 1f);
        view.setImageMatrix(matrix);
        view1.setImageMatrix(matrix);
     }
     @Override
    /* public boolean onTouch(View arg0, MotionEvent arg1) {
  
         // Get the action that was done on this touch event
         switch (arg1.getAction())
         {
             case MotionEvent.ACTION_DOWN:
             {
                 // store the X value when the user's finger was pressed down
                 downXValue = arg1.getX();
                 break;
             }
  
             case MotionEvent.ACTION_UP:
             {
                 // Get the X value when the user released his/her finger
                 float currentX = arg1.getX();            
  
                 // going backwards: pushing stuff to the right
                 if (downXValue &lt; currentX)
                 {
                     // Get a reference to the ViewFlipper
                      ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                      // Set the animation
                      // vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
                       // Flip!
                      if(counter &gt; 0){
  
  
                       vf.showPrevious();
                       counter--;
                 }
                 }
  
                 // going forwards: pushing stuff to the left
                 if (downXValue &gt; currentX)
                 {
                     // Get a reference to the ViewFlipper
                     ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                      // Set the animation
                     //vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
                       // Flip!
                     if(counter &lt; 1){
                      vf.showNext();
                      counter++;
                     }
                 }
                 break;
             }
         }
  
         // if you return false, these actions will not be recorded
         return true;
     }*/
     public boolean onTouch(View v, MotionEvent rawEvent) {
        WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent);
        // ...
        ImageView view = (ImageView) v;
  
        // Dump touch event to log
        dumpEvent(event);
  
        // Handle touch events here...
        switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
           savedMatrix.set(matrix);
           start.set(event.getX(), event.getY());
           Log.d(TAG, "mode=DRAG");
           mode = DRAG;
           downXValue = rawEvent.getX();
           break;
        case MotionEvent.ACTION_POINTER_DOWN:
           oldDist = spacing(event);
           Log.d(TAG, "oldDist=" + oldDist);
           if (oldDist &gt; 10f) {
              savedMatrix.set(matrix);
              midPoint(mid, event);
              mode = ZOOM;
              Log.d(TAG, "mode=ZOOM");
           }
           break;
        case MotionEvent.ACTION_UP:
        {
            // Get the X value when the user released his/her finger
            float currentX = rawEvent.getX();            
  
            // going backwards: pushing stuff to the right
            if (downXValue &lt; currentX)
            {
                // Get a reference to the ViewFlipper
                 ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                 // Set the animation
                 // vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
                  // Flip!
                 if(counter &gt; 0){
  
  
                  vf.showPrevious();
                  counter--;
            }
            }
  
            // going forwards: pushing stuff to the left
            if (downXValue &gt; currentX)
            {
                // Get a reference to the ViewFlipper
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                 // Set the animation
                //vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
                  // Flip!
                if(counter &lt; 1){
                 vf.showNext();
                 counter++;
                }
            }
        }
        case MotionEvent.ACTION_POINTER_UP:
           mode = NONE;
           Log.d(TAG, "mode=NONE");
  
           break;
        case MotionEvent.ACTION_MOVE:
           if (mode == DRAG) {
              // ...
              matrix.set(savedMatrix);
              matrix.postTranslate(event.getX() - start.x,
                    event.getY() - start.y);
           }
           else if (mode == ZOOM) {
              float newDist = spacing(event);
              Log.d(TAG, "newDist=" + newDist);
              if (newDist &gt; 10f) {
                 matrix.set(savedMatrix);
                 float scale = newDist / oldDist;
                 matrix.postScale(scale, scale, mid.x, mid.y);
              }
           }
           break;
        }
  
        view.setImageMatrix(matrix);
        return true; // indicate event was handled
     }
  
     /** Show an event in the LogCat view, for debugging */
     private void dumpEvent(WrapMotionEvent event) {
        // ...
        String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
              "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
        StringBuilder sb = new StringBuilder();
        int action = event.getAction();
        int actionCode = action &amp; MotionEvent.ACTION_MASK;
        sb.append("event ACTION_").append(names[actionCode]);
        if (actionCode == MotionEvent.ACTION_POINTER_DOWN
              || actionCode == MotionEvent.ACTION_POINTER_UP) {
           sb.append("(pid ").append(
                 action &gt;&gt; MotionEvent.ACTION_POINTER_ID_SHIFT);
           sb.append(")");
        }
        sb.append("[");
        for (int i = 0; i &lt; event.getPointerCount(); i++) {
           sb.append("#").append(i);
           sb.append("(pid ").append(event.getPointerId(i));
           sb.append(")=").append((int) event.getX(i));
           sb.append(",").append((int) event.getY(i));
           if (i + 1 &lt; event.getPointerCount())
              sb.append(";");
        }
        sb.append("]");
        Log.d(TAG, sb.toString());
     }
  
     /** Determine the space between the first two fingers */
     private float spacing(WrapMotionEvent event) {
        // ...
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
     }
  
     /** Calculate the mid point of the first two fingers */
     private void midPoint(PointF point, WrapMotionEvent event) {
        // ...
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
     }
  }
  </code></pre>