我有以下異常:DayDream中的VideoView和MediaController?
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
背景下,這是一個DreamService
內的Window
。我真的不想從服務中啓動一個Activity(這會破壞白日夢的意義,不是嗎?),但我也不想重新實現VideoView。
雖然我最有可能不得不去另一條路線而不是VideoView,並希望它們不要使用下面的對話框(MediaPlayer和SurfaceView)。
有關如何解決在DreamService
的Window
中使用VideoView
而未獲得BadTokenException
的問題的任何想法?