gpt4 book ai didi

android - 应用程序显示错误无法添加窗口—— token null 无效;你的 Activity 在运行吗?

转载 作者:行者123 更新时间:2023-11-30 00:50:01 25 4
gpt4 key购买 nike

我想在服务的弹出窗口中显示视频,但是当我启动服务时,我的应用程序崩溃并显示此错误:-

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

我的视频播放器服务代码在这里:

VideoExtractor VideoExtractor = VideoExtractor.create();
VideoExtractor.extract("FTD9aA4S5BY").enqueue(new Callback<VideoExtractionResult>() {
@Override
public void onResponse(Call<YouTubeExtractionResult> call, Response<VideoExtractionResult> response) {
Log.d(TAG, response.body().getVideoUri().toString());
final VideoView mVideoview = (VideoView)mView.findViewById(R.id.youtubePlayer);
mVideoview.setMediaController(new MediaController(PlayerService.this))
mVideoview.setVideoURI(response.body().getVideoUri());
mVideoview.start();

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
mWindowManager.addView(mView, params);
}

@Override
public void onFailure(Call<YouTubeExtractionResult> call, Throwable t) {

}
});

我在 myactivity on resume 方法中调用此服务。

此错误的错误堆栈在这里:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:574)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:86)
at android.widget.MediaController.show(MediaController.java:379)
at android.widget.MediaController.show(MediaController.java:329)
at android.widget.VideoView$2.onPrepared(VideoView.java:454)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2815)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

为什么会出现此错误以及如何解决。

最佳答案

“WindowManager$BadTokenException”这个错误发生在你使用像对话框这样的窗口级对象时,并且在结束你的 Activity 之前,这个错误就发生了。所以你应该设置 Activity 是否被破坏的条件,然后请使用上下文。

关于android - 应用程序显示错误无法添加窗口—— token null 无效;你的 Activity 在运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41217583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com