Skip to content

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

Open
@pansong291

Description

我是在一个全局悬浮窗里面添加的这个组件,所以没有 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)

Activity

pansong291

pansong291 commented on Oct 21, 2024

@pansong291
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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      使用 Application 创建后显示 bubble 时报错 · Issue #214 · woxingxiao/BubbleSeekBar