我正在编写一个 Android 应用程序,它有一个介绍视频,该视频是一个首先播放的 mp4 文件。用户可以触摸屏幕跳过它。
当我尝试在 Android 模拟器上运行我的应用程序时,我收到“Activity 已泄漏窗口.. 最初添加的”异常。在 stackoverflow 上有一篇关于它的大文章说如果我的东西在完成后试图启动一个对话框。也许在我的情况下,播放器试图启动一个对话框,提示播放器不支持我的视频格式。
好的..所以我只想跳过视频。而不会抛出错误和异常。
这是我的介绍 Activity :
package com.tuxin.findasmile;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
//import android.view.Menu;
import android.view.MotionEvent;
import android.widget.VideoView;
public class IntroActivity extends Activity {
private VideoView video;
private boolean isMenuStarted=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
video = (VideoView) findViewById(R.id.videoView1);
video.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
Intent menu = new Intent(IntroActivity.this,MenuActivity.class);
startActivity(menu);
finish();
return false;
}
});
video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (!isMenuStarted) {
isMenuStarted=true;
Intent menu = new Intent(IntroActivity.this,MenuActivity.class);
startActivity(menu);
finish();
}
}
});
Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.seaofbuttons);
video.setVideoURI(videoUri);
video.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (video.isPlaying()) {
video.stopPlayback();
}
if (!isMenuStarted) {
isMenuStarted=true;
Intent menu = new Intent(IntroActivity.this,MenuActivity.class);
startActivity(menu);
finish();
}
return false;
}
}
我知道模拟器不支持视频。但我想支持手机不支持这种视频格式的情况。我不想抛出异常。
04-01 16:32:38.675: E/WindowManager(533): Activity com.tuxin.findasmile.IntroActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4052b9b0 that was originally added here
04-01 16:32:38.675: E/WindowManager(533): android.view.WindowLeaked: Activity com.tuxin.findasmile.IntroActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4052b9b0 that was originally added here
04-01 16:32:38.675: E/WindowManager(533): at android.view.ViewRoot.<init>(ViewRoot.java:258)
04-01 16:32:38.675: E/WindowManager(533): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-01 16:32:38.675: E/WindowManager(533): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-01 16:32:38.675: E/WindowManager(533): at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-01 16:32:38.675: E/WindowManager(533): at android.app.Dialog.show(Dialog.java:241)
04-01 16:32:38.675: E/WindowManager(533): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
04-01 16:32:38.675: E/WindowManager(533): at android.widget.VideoView$4.onError(VideoView.java:386)
04-01 16:32:38.675: E/WindowManager(533): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1341)
04-01 16:32:38.675: E/WindowManager(533): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 16:32:38.675: E/WindowManager(533): at android.os.Looper.loop(Looper.java:123)
04-01 16:32:38.675: E/WindowManager(533): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-01 16:32:38.675: E/WindowManager(533): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 16:32:38.675: E/WindowManager(533): at java.lang.reflect.Method.invoke(Method.java:507)
04-01 16:32:38.675: E/WindowManager(533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-01 16:32:38.675: E/WindowManager(533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-01 16:32:38.675: E/WindowManager(533): at dalvik.system.NativeStart.main(Native Method)
最佳答案
尽管我认为这个问题的答案会很复杂。 我需要做的就是在 onError 函数中返回 true。
问题是它试图显示一个对话框,其中包含无法观看视频的错误消息。我没有看到它,因为我已经转到下一个 Activity ,所以这个对话框被泄露了,我看到了这个异常。
将 onError 返回值从 false 更改为 true 将改变 VideoView 的行为,不再创建带有消息的对话框,因此不会泄露任何内容。
关于java - "Activity has leaked window... that was originally added."视频加载失败导致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15744666/