确认单击时手势崩溃

时间:2019-07-05 18:11:40

标签: android android-custom-view gesturedetector

  

单次点击,我的应用程序崩溃-> Firebase Crashlytics

Fatal Exception: java.lang.VerifyError: megaminds/easytouch/easytouch/adapters/PanelAdapter
       at megaminds.easytouch.easytouch.views.MainLayout.initializeHomePanel + 234(MainLayout.java:234)
       at megaminds.easytouch.easytouch.views.MainLayout.handlePhuntruTouchListener + 564(MainLayout.java:564)
       at megaminds.easytouch.easytouch.views.MainLayout.access$000 + 72(MainLayout.java:72)
       at megaminds.easytouch.easytouch.views.MainLayout$1.onClick + 142(MainLayout.java:142)
       at megaminds.easytouch.easytouch.views.CustomButtonNew.onSingleTapConfirmed + 200(CustomButtonNew.java:200)
       at android.support.v4.view.GestureDetectorCompat$GestureDetectorCompatImplBase$GestureHandler.handleMessage + 126(GestureDetectorCompat.java:126)
       at android.os.Handler.dispatchMessage + 107(Handler.java:107)
       at android.os.Looper.loop + 194(Looper.java:194)
       at android.app.ActivityThread.main + 5427(ActivityThread.java:5427)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke + 525(Method.java:525)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 842(ZygoteInit.java:842)
       at com.android.internal.os.ZygoteInit.main + 609(ZygoteInit.java:609)
       at dalvik.system.NativeStart.main(NativeStart.java)

CustomButtonNew.class

public class CustomButtonNew extends AppCompatImageButton implements GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener { //ImageButton

    private Context mContext;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams params;

    private OnMultiEventListener onMultiEventListener;
    private GestureDetectorCompat mDetector;
    private static final String DEBUG_TAG = "Gestures";

    int lastX, lastY;
    int paramX, paramY;

    public CustomButtonNew(Context context) {
        super(context);
        initView(context);
    }

    public CustomButtonNew(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomButtonNew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {

        int[] attrs = new int[]{R.attr.selectableItemBackground};
        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        int backgroundResource = typedArray.getResourceId(0, 0);
        this.setBackgroundResource(backgroundResource);
        typedArray.recycle();

        setClickable(true);
        setFocusable(true);

        mContext = context;
        //mCustomButton = this;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        params = getBtnParams();
        mDetector = new GestureDetectorCompat(mContext,this);
        // Set the gesture detector as the double tap
        // listener.
        mDetector.setOnDoubleTapListener(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (canDrawOverlayViews(mContext)) {
                mWindowManager.addView(this,params);
            }
        }
    }


    public void setOnMultiEventListener(OnMultiEventListener onMultiEventListener) {
        this.onMultiEventListener = onMultiEventListener;
    }

    private WindowManager.LayoutParams getBtnParams() {
        // FIXME: 11/29/2018 https://stackoverflow.com/questions/32224452/android-unable-to-add-window-permission-denied-for-this-window-type/32233473 plz read..
        int LAYOUT_FLAG = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.
                LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

        float factor = this.getContext().getResources().getDisplayMetrics().density;
        int dimensionInPixel = SharedPrefs.read(Constants.TOUCHER_SIZE,0) + 40;// added 50 for increasing it's initial size
        int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());

        /*DisplayMetrics metrics = this.getResources().getDisplayMetrics();
        int screenWidth = metrics.widthPixels;
        int screenHeight = metrics.heightPixels;*/

        //int widthNHeight = ViewOp.dpToPx(mContext,dimensionInDp);

        /*layoutParams.height = dpToPx(250);
        layoutParams.width = dpToPx(250);*/

        WindowManager.LayoutParams btnParams = new WindowManager.LayoutParams(
                dimensionInDp,
                dimensionInDp,
                // FIXME: 5/1/2019 update phuntru size from here...
                /*SharedPrefs.read(Constants.TOUCHER_SIZE,dimensionInDp),
                SharedPrefs.read(Constants.TOUCHER_SIZE,dimensionInDp),*/
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);
        btnParams.gravity = Gravity.LEFT | Gravity.TOP;

        /*btnParams.height = dimensionInDp;
        btnParams.width = dimensionInDp;*/

        btnParams.x = 0;
        btnParams.y = 0;
        return btnParams;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.mDetector.onTouchEvent(event)) {
            return true;
        }
        ///getBackground().setAlpha(255);
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent event) {
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
        paramX = params.x;
        paramY = params.y;
        return true;
        /*Log.d(DEBUG_TAG,"onDown: " + event.toString());
        return true;*/
    }

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2,
                           float velocityX, float velocityY) {
        Log.d(DEBUG_TAG, "onFling: " + event1.toString() + event2.toString());
        return true;
    }

    @Override
    public void onLongPress(MotionEvent event) {
        Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
        if(onMultiEventListener != null)
            onMultiEventListener.onLongPressed();
    }

    @Override
    public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX,
                            float distanceY) {
        int dx = (int) event2.getRawX() - lastX;
        int dy = (int) event2.getRawY() - lastY;
        params.x = paramX + dx;
        params.y = paramY + dy;
        // 更新悬浮窗位置
        mWindowManager.updateViewLayout(this, params);
        ///getBackground().setAlpha(30);
        return true;
        /*Log.d(DEBUG_TAG, "onScroll: " + event1.toString() + event2.toString());
        return true;*/
    }

    @Override
    public void onShowPress(MotionEvent event) {
        Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent event) {
        Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent event) {
        Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
        if(onMultiEventListener != null)
            onMultiEventListener.onDoubleClick();
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
        try {
            Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
            if(onMultiEventListener != null)
                onMultiEventListener.onClick();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public void removeFromWindowManager(){
        try {
            mWindowManager.removeView(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我已应用try catch,但该行仍然崩溃,但是在调试过程中无法创建它。

0 个答案:

没有答案