类中的Android Gesture Detection,调用方法

标签 android class gesture

我尝试检测 android 中的手势,但它一直崩溃,我在创建构造函数时遇到问题,我在使用类方面不是那么先进

这是 GestureDetector 类,包括 OnGestureListener 的所有必要方法:

public class GestureDetectorClass implements OnGestureListener {

      @Override
      public boolean onDown(MotionEvent arg0) {
          Log.d("Gesture", "onDown");
       return false;
      }

      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY) {
          Log.d("Gesture", "onFling");
      return true;
      }

      @Override
      public void onLongPress(MotionEvent e) {
          Log.d("Gesture", "onLongPress");
      }

      @Override
      public boolean onScroll(MotionEvent e1, MotionEvent e2,
        float distanceX, float distanceY) {
          Log.d("Gesture", "onScroll");
       return false;
      }

      @Override
      public void onShowPress(MotionEvent e) { 
          Log.d("Gesture", "onShowPress");
      }

      @Override
      public boolean onSingleTapUp(MotionEvent e) {
          Log.d("Gesture", "onSingleTapUp");
          return false;
      }

}

主要是我现在尝试调用类/方法 尝试了以下代码:

public class MainActivity extends Activity  {


    GestureDetectorClass myGestureClass = new GestureDetectorClass();
    GestureDetector myDector = new GestureDetector(this, myGestureClass);

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

    }

    public boolean onTouch(MotionEvent e) {
        return myDector.onTouchEvent(e);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

在日志中我发现了这个错误:

    11-13 11:15:02.615: E/AndroidRuntime(1127): Caused by: java.lang.NullPointerException
11-13 11:15:02.615: E/AndroidRuntime(1127):     at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:125)

最佳答案

建议我在设置内容 View 后创建 myDetector 的新对象,因为您在 new GestureDetector(this, myGestureClass); 中使用 this;这可能就是原因对于空指针异常

GestureDetector myDector;
GestureDetectorClass myGestureClass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myGestureClass = new GestureDetectorClass();
    myDector = new GestureDetector(this, myGestureClass);

}

这是一个例子 http://android-er.blogspot.com/2012/07/implement-gesturedetectorongestureliste.html

关于类中的Android Gesture Detection,调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19951263/

相关文章:

c++ - 类函数指针作为静态函数中的参数

php - 在抽象类方法中返回抽象类的子类

swift - 如何使用滑动手势访问相机?

ios - 如何仅在 swiftUI 中将拖动手势限制到特定帧

ios - 如何在iOS上实现页面滑动效果

java - 写一个类似Spring Bean实例化方法 context.getBean ("beanobjectname",Type)

android - BackStack 不适用于嵌套 fragment

android - 从列表中将 LatLon 添加到 Polygon android google map version 2

java - 尝试在 EditText 中将数字设置为文本

Android Loaders 生命周期,或 : will onStopLoading() always called before onReset()?