我想在检测到单点视图时调用startactivity for result。
对于视图中的单点击,我在视图的ontouch方法中编写了以下条件
@Override public boolean onTouchEvent(MotionEvent ev) {
if (!mSupportsZoom && !mSupportsPan) return false;
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
if (mSupportsPan && !mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
//mFocusX = mPosX;
//mFocusY = mPosY;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
final float x = ev.getX();
final float y = ev.getY();
touchupX=x;
touchupY=y;
if(mLastTouchX == touchupX && mLastTouchY == touchupY){ //this is my condition to detect single tap on the view
PinchZoomPanActivity2.tapped(null, 100); //method for startactivityfor result which is in main activity
}
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
主要活动中的抽头方法如下
static void tapped(Activity activity, int requestCode){
activity.startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 1);
}
但这不起作用,因为它排除了错误空指针异常。
请说明我的代码有什么问题。
答案 0 :(得分:2)
PinchZoomPanActivity2.tapped(null, 100);
您为活动传递了null。因此,tapped函数会尝试在其上调用startActivityForResult并因为null而失败。
答案 1 :(得分:0)
最终的代码是 我在我的视图中添加了this.getcontext(),
PinchZoomPanActivity2.tapped(this.getContext(), 100);
以及活动如下
static void tapped(Context context, int requestCode){
((Activity) context).startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 1);
}