2011-06-17 80 views
1

我正在開發Android中的一個小應用程序,我必須顯示圖像。我在我的應用程序中有2個圖像,但我必須通過手指觸摸來移動圖像的功能,因此我在我的應用程序中使用了viewflipper,並且還希望縮放和縮小功能。Android中的應用程序在運行時期間發生致命異常?

我也爲xoom進出3班。但是,當我運行該應用程序時,它會顯示第一個圖像,並且還會在它觸及時將其縮放,然後將其移動到第二個圖像,然後將其翻轉回來並試圖擴大它我的應用程序會崩潰,並在logcat的

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) 

在這裏給下面的消息,我的Java代碼

/*** 
* 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 < 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 > 0){ 


        vf.showPrevious(); 
        counter--; 
       } 
       } 

       // going forwards: pushing stuff to the left 
       if (downXValue > 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 < 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() & 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 > 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 < 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 > 0){ 


       vf.showPrevious(); 
       counter--; 
      } 
      } 

      // going forwards: pushing stuff to the left 
      if (downXValue > 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 < 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 > 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 & 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 >> MotionEvent.ACTION_POINTER_ID_SHIFT); 
     sb.append(")"); 
     } 
     sb.append("["); 
     for (int i = 0; i < 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 < 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); 
    } 
} 
+0

該死的用戶5520272345235423有人可以讀這個 – Bastardo 2011-06-17 10:06:42

+0

你的源代碼在哪裏? – 2011-06-17 10:10:30

回答

1

當你鑄造vImageView收到ClassCastException 。這意味着用戶觸摸了不是ImageView的視圖。

爲了避免代碼(51,52),這個你可以刪除下一行的onCreate方法:

LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main); 
layMain.setOnTouchListener((OnTouchListener) this); 

還是在onTouch方法上線添加額外的檢查116:

if(!(v instanceof ImageView))return false; 
相關問題