touchlistener不工作

9cbw7uwe  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(288)

这是我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:textColor="#8B008B"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Static ZoomButtom" />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />
    <ZoomButton
        android:id="@+id/zoomButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:src="@android:drawable/btn_plus" />
    <ZoomButton
        android:id="@+id/zoomButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/btn_minus"
        android:layout_alignTop="@+id/zoomButton1"
        android:layout_toLeftOf="@+id/textView1"
        android:layout_toStartOf="@+id/textView1" />
</RelativeLayout>

这里我的java类父类是fullscreenactivity,scalelistener是我的内部类。。

package com.adi.hp.adnanapp;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ZoomButton;
public class FullScreenActivity extends AppCompatActivity {
    ZoomButton zoom;
    ZoomButton zoomM;
    ImageView img;
//panning
private static final int INVALID_POINTER_ID = -1;
    private float mPosX;
    private float mPosY;
    private float mLastTouchX;
    private float mLastTouchY;
    private float mLastGestureX;
    private float mLastGestureY;
    private int mActivePointerId = INVALID_POINTER_ID;
    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.f;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen);
        zoomM=(ZoomButton) findViewById(R.id.zoomButton2);
        zoom = (ZoomButton) findViewById(R.id.zoomButton1);
        img = (ImageView) findViewById(R.id.imageView1);

        zoom.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                float x = img.getScaleX();
                float y = img.getScaleY();

                img.setScaleX(x + 2);
                img.setScaleY(y + 2);
            }
        });

        //zoom out
        zoomM.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                float x = img.getScaleX();
                float y = img.getScaleY();

                img.setScaleX(x - 2);
                img.setScaleY(y - 2);
            }
        });
        img.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent ev) {

                mScaleDetector.onTouchEvent(ev);

                final int action = ev.getAction();
                switch (action & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN: {
                        if (!mScaleDetector.isInProgress()) {
                            final float x = ev.getX();
                            final float y = ev.getY();

                            mLastTouchX = x;
                            mLastTouchY = y;
                            mActivePointerId = ev.getPointerId(0);
                        }
                        break;
                    }
                    case MotionEvent.ACTION_POINTER_1_DOWN: {
                        if (mScaleDetector.isInProgress()) {
                            final float gx = mScaleDetector.getFocusX();
                            final float gy = mScaleDetector.getFocusY();
                            mLastGestureX = gx;
                            mLastGestureY = gy;
                        }
                        break;
                    }
                    case MotionEvent.ACTION_MOVE: {

                        // Only move if the ScaleGestureDetector isn't processing a gesture.
                        if (!mScaleDetector.isInProgress()) {
                            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
                            final float x = ev.getX(pointerIndex);
                            final float y = ev.getY(pointerIndex);

                            final float dx = x - mLastTouchX;
                            final float dy = y - mLastTouchY;

                            mPosX += dx;
                            mPosY += dy;

                            //invalidate();

                            mLastTouchX = x;
                            mLastTouchY = y;
                        }
                        else{
                            final float gx = mScaleDetector.getFocusX();
                            final float gy = mScaleDetector.getFocusY();

                            final float gdx = gx - mLastGestureX;
                            final float gdy = gy - mLastGestureY;

                            mPosX += gdx;
                            mPosY += gdy;

                            //invalidate();

                            mLastGestureX = gx;
                            mLastGestureY = gy;
                        }

                        break;
                    }
                    case MotionEvent.ACTION_UP: {
                        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) {
                            // This was our active pointer going up. Choose a new
                            // active pointer and adjust accordingly.
                            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                            mLastTouchX = ev.getX(newPointerIndex);
                            mLastTouchY = ev.getY(newPointerIndex);
                            mActivePointerId = ev.getPointerId(newPointerIndex);
                        }
                        else{
                            final int tempPointerIndex = ev.findPointerIndex(mActivePointerId);
                            mLastTouchX = ev.getX(tempPointerIndex);
                            mLastTouchY = ev.getY(tempPointerIndex);
                        }

                        break;
                    }
                }

                return true;
            }
        });

        }
    public FullScreenActivity() {

    }
    public FullScreenActivity(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    }

    public FullScreenActivity(Context context, AttributeSet attrs, int defStyle) {
        //super(context, attrs, defStyle);

        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    }

     class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();

            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));

           // invalidate();
            return true;
        }
    }

}

日志:

01-04 01:37:16.096    2530-2530/com.adi.hp.adnanapp E/InputEventReceiver﹕ Exception dispatching input event.
01-04 01:37:16.096    2530-2530/com.adi.hp.adnanapp E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
01-04 01:37:16.116    2530-2530/com.adi.hp.adnanapp E/MessageQueue-JNI﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.ScaleGestureDetector.onTouchEvent(android.view.MotionEvent)' on a null object reference
            at com.adi.hp.adnanapp.FullScreenActivity$3.onTouch(FullScreenActivity.java:79)
            at android.view.View.dispatchTouchEvent(View.java:8499)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2390)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1734)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2833)
            at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2351)
            at android.view.View.dispatchPointerEvent(View.java:8700)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4415)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4281)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3779)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3832)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3798)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3915)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3806)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3972)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3779)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3832)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3798)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3806)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3779)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6125)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6080)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6051)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6226)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:143)
            at android.os.Looper.loop(Looper.java:122)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
01-04 01:37:16.116    2530-2530/com.adi.hp.adnanapp D/AndroidRuntime﹕ Shutting down VM
01-04 01:37:16.116    2530-2530/com.adi.hp.adnanapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.adi.hp.adnanapp, PID: 2530
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.ScaleGestureDetector.onTouchEvent(android.view.MotionEvent)' on a null object reference
            at com.adi.hp.adnanapp.FullScreenActivity$3.onTouch(FullScreenActivity.java:79)
            at android.view.View.dispatchTouchEvent(View.java:8499)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2390)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1734)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2833)
            at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2351)
            at android.view.View.dispatchPointerEvent(View.java:8700)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4415)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4281)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3779)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3832)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3798)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3915)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3806)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3972)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3779)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3832)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3798)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3806)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3779)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6125)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6080)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6051)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6226)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:143)
            at android.os.Looper.loop(Looper.java:122)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

请任何人帮忙。提前谢谢。。。

h43kikqp

h43kikqp1#

它告诉你是因为你打电话 onTouchEvent 在一个空对象上引用,所以我认为它是mscaledector。您没有在默认构造函数中示例化它,因此它可以为null。
我建议将其示例化移到 onCreate 方法。看看这个问题

相关问题