我得到以下异常:
02-10 15:30:03.676 12841-12841/hu.stuff.dreamE/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: hu.stuff.dream, PID: 12841
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:536)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
at android.widget.VideoView$5.onError(VideoView.java:516)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2248)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5034)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
对于以下代码:
public class MyDreamService extends DreamService {
VideoView vidView;
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setInteractive(false);
setFullscreen(true);
setContentView(R.layout.activity_main);
}
@Override
public void onDreamingStarted() {
super.onDreamingStarted();
vidView = (VideoView)findViewById(R.id.myVideo);
vidView.setVideoPath("Voice_Activated_Corgi.mp4");
vidView.start();
}
@Override
public void onDreamingStopped() {
super.onDreamingStopped();
vidView.stopPlayback();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
}
很明显,问题在于这不是Activity
上下文,这是 a Window
within a DreamService
。我真的不想从服务中启动一个活动(那会毁了白日梦,不是吗?),但我也不想重新实现 VideoView。
虽然我很可能不得不走另一条路线而不是 VideoView,并希望他们不要使用下面的对话框(MediaPlayer 和 SurfaceView)。
关于如何解决在没有得到的情况下使用的问题VideoView
的任何DreamService
想法?Window
BadTokenException