1

I created a custom button that shows a PopupWindow when touched. This works perfectly on all my devices as well as the Android emulator. However, I sometimes get crash reports on Google Developer Console, but unfortunately I can't reproduce it and Google Developer Console doesn't say the reason.

The exception occurs at this line:

popupWindow.showAtLocation(customView, Gravity.NO_GRAVITY, location.left, location.top);

CustomButton:

public class CustomButton extends Button {

    CustomView customView;
    PopupWindow popupWindow;
    int[] viewLocation;
    Rect location;

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        customView = new CustomView(context);
        popupWindow = new PopupWindow(customView);
        viewLocation = new int[2];
        location = new Rect();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        popupWindow.setWidth((int) (this.getWidth() * 1.4));
        popupWindow.setHeight((int) (this.getHeight() * 1.2));
        this.getLocationOnScreen(viewLocation);
        location.left = viewLocation[0] - (popupWindow.getWidth() - this.getWidth()) / 2;
        location.top = viewLocation[1] - popupWindow.getHeight();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                this.setPressed(true);
                this.performClick();
                popupWindow.showAtLocation(customView, Gravity.NO_GRAVITY, location.left, location.top);
                break;
            case MotionEvent.ACTION_UP:
                this.setPressed(false);
                popupWindow.dismiss();
                break;
        }
        return true;
    }
}

Crash Report:

java.lang.IllegalStateException: 
  at android.view.View$1.onClick (View.java:4028)
  at android.view.View.performClick (View.java:4788)
  at com.company.appname.CustomButton.onTouchEvent (CustomButton.java:43)
  at android.view.View.dispatchTouchEvent (View.java:8502)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2434)
  at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2066)
  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent (PhoneWindow.java:2482)
  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1780)
  at android.app.Activity.dispatchTouchEvent (Activity.java:2826)
  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent (PhoneWindow.java:2441)
  at android.view.View.dispatchPointerEvent (View.java:8709)
  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent (ViewRootImpl.java:4223)
  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess (ViewRootImpl.java:4077)
  at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3618)
  at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3677)
  at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3643)
  at android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:3760)
  at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3651)
  at android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:3817)
  at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3618)
  at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3677)
  at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3643)
  at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3651)
  at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3618)
  at android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:5942)
  at android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:5916)
  at android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:5883)
  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent (ViewRootImpl.java:6039)
  at android.view.InputEventReceiver.dispatchInputEvent (InputEventReceiver.java:193)
  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:5425)
  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:928)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:723)
Caused by: java.lang.reflect.InvocationTargetException: 
  at java.lang.reflect.Method.invoke (Native Method)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at android.view.View$1.onClick (View.java:4023)

What's wrong here?

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
  • Did you try solutions from other similar questions? There're lots of them: https://stackoverflow.com/questions/15433064/illegalstateexception-when-click-on-button-in-android-hello-world-program https://stackoverflow.com/questions/23426999/androidonclick-causes-illegalstateexception https://stackoverflow.com/questions/36502277/getting-illegalstateexception-on-button-click – eleven Oct 08 '17 at 00:24
  • @eleven None of them is similar to my question. They are all different. Anyway, yes I have searched a lot about this issue, but I did not find any result. – Hussein El Feky Oct 08 '17 at 00:30
  • @HusseinElFeky this is a long shot but getWidth added in API level 1 int getWidth () This method was deprecated in API level 13. Use getSize(Point) instead. – Vector Oct 08 '17 at 02:28
  • @Grendel AFAIK, `getWidth` was only deprecated in `Display` not in `View`. – Hussein El Feky Oct 08 '17 at 09:21
  • @HusseinElFeky I stand corrected – Vector Oct 08 '17 at 15:19
  • @Grendel Are you sure of that? Because apparently Android Studio Lint doesn't show that it is deprecated, and when I checked Android documentation, it was only deprecated in `Display`. From where did you get this? – Hussein El Feky Oct 08 '17 at 16:50
  • @HusseinElFeky Did some searching when I was looking at using Display Metrics a very old post something about only working below 19 – Vector Oct 08 '17 at 17:03

0 Answers0