Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 Application 创建后显示 bubble 时报错 #214

Open
pansong291 opened this issue Oct 20, 2024 · 1 comment
Open

使用 Application 创建后显示 bubble 时报错 #214

pansong291 opened this issue Oct 20, 2024 · 1 comment

Comments

@pansong291
Copy link

我是在一个全局悬浮窗里面添加的这个组件,所以没有 Activity,当开始拖动时,报错:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:798)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
    at com.xw.repo.BubbleSeekBar$7.onAnimationStart(BubbleSeekBar.java:1020)
    at android.view.ViewPropertyAnimator$AnimatorEventListener.onAnimationStart(ViewPropertyAnimator.java:1090)
    at android.animation.Animator$AnimatorListener.onAnimationStart(Animator.java:536)
    at android.animation.ValueAnimator.notifyStartListeners(ValueAnimator.java:1013)
    at android.animation.ValueAnimator.startAnimation(ValueAnimator.java:1262)
    at android.animation.ValueAnimator.start(ValueAnimator.java:1064)
    at android.animation.ValueAnimator.start(ValueAnimator.java:1088)
    at android.view.ViewPropertyAnimator.startAnimation(ViewPropertyAnimator.java:904)
    at android.view.ViewPropertyAnimator.start(ViewPropertyAnimator.java:416)
    at com.xw.repo.BubbleSeekBar.showBubble(BubbleSeekBar.java:1022)
    at com.xw.repo.BubbleSeekBar.onTouchEvent(BubbleSeekBar.java:695)
@pansong291
Copy link
Author

主要变更:

if (context instanceof Application) {
    // 使用全局悬浮窗
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    }
} else {
    // MIUI禁止了开发者使用TYPE_TOAST,Android 7.1.1 对TYPE_TOAST的使用更严格
    if (BubbleUtils.isMIUI() || Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
    } else {
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant