我创建了一个自定义按钮,在触摸时会显示PopupWindow
。这适用于我的所有设备以及Android模拟器。但是,我有时会在Google Developer Console上收到崩溃报告,但遗憾的是我无法重现它,Google Developer Console也没有说明原因。
此行发生异常:
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;
}
}
崩溃报告:
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)
这里有什么问题?