2013-01-12 47 views
3

工作下面是裝載frameanimation的用於在不同events.First事件的發生兩個不同的圖像的代碼被而活動start.Others是onTouch()其中i爲onDown()onScroll()的利用的GestureDetector問題是我得到NullPointerException,而它的OnScroll()。我發現它沒有得到Y座標,可能是什麼問題。的NullPointerException同時用GestureDetector

package com.example.animationtry; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.widget.ImageView; 

public class MainActivity extends Activity 
{ 
    ImageView img,img1; 
    AnimationDrawable animation; 
    private GestureDetector gestureDetector; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     img=(ImageView)findViewById(R.id.image); 
     img.post(new Runnable() 
     { 
      public void run() 
      { 
       img.setBackgroundResource(R.anim.frameanimation); 
       AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); 
       frameAnimation.setVisible(false, true); 
       frameAnimation.start(); 
      } 
     }); 
     img.setOnTouchListener(new View.OnTouchListener() 
     { 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       String img="img"; 
       gestureDetector = new GestureDetector(new MyGestureDetector(img)); 
       if (gestureDetector.onTouchEvent(event)) 
       { 
        return true; 
       } 
       return false; 
      } 
     }); 

     img1=(ImageView)findViewById(R.id.image1); 
     img.post(new Runnable() 
     { 
      public void run() 
      { 
       img1.setBackgroundResource(R.anim.frameanimation); 
       AnimationDrawable frameAnimation = (AnimationDrawable) img1.getBackground(); 
       frameAnimation.setVisible(false, true); 
       frameAnimation.start(); 
      } 
     }); 
     img1.setOnTouchListener(new View.OnTouchListener() 
     { 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       String img1="img1"; 
       gestureDetector = new GestureDetector(new MyGestureDetector(img1)); 
       if (gestureDetector.onTouchEvent(event)) 
       { 
        return true; 
       } 
       return false; 
      } 
     }); 
    } 

    class MyGestureDetector extends SimpleOnGestureListener 
    { 
     private static final int SWIPE_MAX_OFF_PATH = 40; 
     private static final int SWIPE_THRESHOLD_VELOCITY = 30; 
     String viewname; 
     public MyGestureDetector(String view) 
     { 
      viewname=view; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) 
     { 
      return true; 
     } 
     @Override 
     public boolean onDown(MotionEvent e) 
     { 
      Log.i("View Name",viewname); 
      if(viewname.equalsIgnoreCase("img")) 
      { 
       img.setBackgroundResource(R.anim.clickanimation); 
       animation = (AnimationDrawable)img.getBackground(); 
       animation.setVisible(false, true); 
       animation.start(); 
      } 
      else if(viewname.equalsIgnoreCase("img1")) 
      { 
       img1.setBackgroundResource(R.anim.clickanimation); 
       animation = (AnimationDrawable)img1.getBackground(); 
       animation.setVisible(false, true); 
       animation.start(); 
      } 
      return true; 
     } 

     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) 
     { 
      Log.i("y1",Float.toString(e1.getY())); 
      Log.i("y2",Float.toString(e2.getY())); 
      Log.i("Drag View Name",viewname); 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH && Math.abs(distanceY) > SWIPE_THRESHOLD_VELOCITY) 
      { 
       if(viewname.equalsIgnoreCase("img")) 
       { 
        img.setBackgroundResource(R.anim.draganimaton); 
        animation = (AnimationDrawable)img.getBackground(); 
        animation.setVisible(false, true); 
        animation.start(); 
       } 
       else if(viewname.equalsIgnoreCase("img1")) 
       { 
        img1.setBackgroundResource(R.anim.draganimaton); 
        animation = (AnimationDrawable)img1.getBackground(); 
        animation.setVisible(false, true); 
        animation.start(); 
       } 
       return true; 
      } 
      return true; 
     } 
    } 
} 

以下是logcat的:

01-12 06:12:22.637: E/AndroidRuntime(1062): FATAL EXCEPTION: main 
01-12 06:12:22.637: E/AndroidRuntime(1062): java.lang.NullPointerException 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at com.example.animationtry.MainActivity$MyGestureDetector.onScroll(MainActivity.java:117) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.view.GestureDetector.onTouchEvent(GestureDetector.java:541) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at com.example.animationtry.MainActivity$4.onTouch(MainActivity.java:69) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.view.View.dispatchTouchEvent(View.java:3881) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.os.Looper.loop(Looper.java:123) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at java.lang.reflect.Method.invoke(Method.java:507) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
01-12 06:12:22.637: E/AndroidRuntime(1062):  at dalvik.system.NativeStart.main(Native Method) 
+0

發佈您的logcat –

+0

已更新que stion – sankettt

+0

哦,得到你想說的話......你的意思是在onTouch()右側返回true。我做到了,但它又給了我同樣的錯誤.. – sankettt

回答

2

我爲onScroll()返回空值的問題e1.getY(),所以我刪除的檢查部分,只是檢查SWIPE_MAX_OFF_PATHonScroll()和現在它的distanceY完美工作..

package com.example.animationtry; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.widget.ImageView; 

public class MainActivity extends Activity 
{ 
    ImageView img,img1; 
    AnimationDrawable animation; 
    private GestureDetector gestureDetector; 
    private static final int SWIPE_MAX_OFF_PATH = 40; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 30; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     img=(ImageView)findViewById(R.id.image); 
     img.post(new Runnable() 
     { 
      public void run() 
      { 
       img.setBackgroundResource(R.anim.frameanimation); 
       AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); 
       frameAnimation.setVisible(false, true); 
       frameAnimation.start(); 
      } 
     }); 
     img.setOnTouchListener(new View.OnTouchListener() 
     { 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       String img="img"; 
       gestureDetector = new GestureDetector(new MyGestureDetector(img)); 
       if (gestureDetector.onTouchEvent(event)) 
       { 
        return true; 
       } 
       return false; 
      } 
     }); 

     img1=(ImageView)findViewById(R.id.image1); 
     img.post(new Runnable() 
     { 
      public void run() 
      { 
       img1.setBackgroundResource(R.anim.frameanimation); 
       AnimationDrawable frameAnimation = (AnimationDrawable) img1.getBackground(); 
       frameAnimation.setVisible(false, true); 
       frameAnimation.start(); 
      } 
     }); 
     img1.setOnTouchListener(new View.OnTouchListener() 
     { 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       String img1="img1"; 
       gestureDetector = new GestureDetector(new MyGestureDetector(img1)); 
       if (gestureDetector.onTouchEvent(event)) 
       { 
        return true; 
       } 
       return false; 
      } 
     }); 
    } 

    class MyGestureDetector extends SimpleOnGestureListener 
    { 
     String viewname; 
     public MyGestureDetector(String view) 
     { 
      viewname=view; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) 
     { 

      return true; 
     } 
     @Override 
     public boolean onDown(MotionEvent e) 
     { 
      Log.i("View Name",viewname); 
      if(viewname.equalsIgnoreCase("img")) 
      { 
       img.setBackgroundResource(R.anim.clickanimation); 
       animation = (AnimationDrawable)img.getBackground(); 
       animation.setVisible(false, true); 
       animation.start(); 
      } 
      else if(viewname.equalsIgnoreCase("img1")) 
      { 
       img1.setBackgroundResource(R.anim.clickanimation); 
       animation = (AnimationDrawable)img1.getBackground(); 
       animation.setVisible(false, true); 
       animation.start(); 
      } 
      return true; 
     } 

     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) 
     {   
      Log.i("Drag View Name",viewname); 
      if (Math.abs(distanceY)>SWIPE_MAX_OFF_PATH) 
      { 
       Log.i("inside","drag"); 
       if(viewname.equalsIgnoreCase("img")) 
       { 
        img.setBackgroundResource(R.anim.draganimaton); 
        animation = (AnimationDrawable)img.getBackground(); 
        animation.setVisible(false, true); 
        animation.start(); 
       } 
       else if(viewname.equalsIgnoreCase("img1")) 
       { 
        img1.setBackgroundResource(R.anim.draganimaton); 
        animation = (AnimationDrawable)img1.getBackground(); 
        animation.setVisible(false, true); 
        animation.start(); 
       } 
      } 
      return true; 
     } 
    } 
} 
+0

在我的情況下,距離不會超過+ -3。我在它的父視圖上使用ListView和處理手勢。 – CoDe

相關問題