Question

I want to invoke startactivity for result when a singletap on view is detected.

For singletap on the view, i wrote the follwing condition in ontouch method of the view

@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;
}

and the tapped method in mainactivity is as follows

static void tapped(Activity activity, int requestCode){

activity.startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 1);
}

but this does not work as it forecloses with error null pointer exception.

Please suggest what is wrong with my code.

Was it helpful?

Solution 2

The final code which worked is I added this.getcontext() in my view,

PinchZoomPanActivity2.tapped(this.getContext(), 100);

and in activity as follows

static void tapped(Context context, int requestCode){

((Activity) context).startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 1);
}

OTHER TIPS

PinchZoomPanActivity2.tapped(null, 100); 

You're passing in null for the activity. So the tapped function tries to call startActivityForResult on it and fails because its null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top