我想在启动时创建启动画面。我还创建了一个activity_splash.xml
。
这是我的Java代码:
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
VideoView videoHolder = new VideoView(this);
setContentView(videoHolder);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash);
videoHolder.setVideoURI(video);
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
jump();
}
});
videoHolder.start();
} catch (Exception ex) {
jump();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
jump();
return true;
}
private void jump() {
if (isFinishing())
return;
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
这是我的AndroidManifest.xml
<activity android:name=".SplashScreen"
android:configChanges="orientation"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我想将VideoView链接到此activity_splash.xml
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="me.smash_it.smashit.SplashScreen">
<VideoView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_centerInParent="true"/>
</RelativeLayout>
我在我的Java代码中试过这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
try {
setContentView(R.layout.activity_splash);
VideoView videoHolder = (VideoView) this.findViewById(R.id.videoView);
VideoView videoHolder = new VideoView(this);
.....
}
当我尝试它没有显示我的Splashscreen它直接启动我的其他活动,那就是Webview。如果看看我的调试我得到了这个错误:
11-07 08:16:49.591 7974-8029 / [PROJECT_NAME] E / libEGL:validate_display:99错误3008(EGL_BAD_DISPLAY)
有谁知道我做错了什么或错误意味着什么?我已经在真实设备上进行了测试,但我仍然遇到了这个错误。
提前致谢!
答案 0 :(得分:0)
我试过了:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
try {
VideoView videoHolder = (VideoView)findViewById(R.id.videoView);
setContentView(videoHolder);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash);
videoHolder.setVideoURI(video);
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
jump();
}
});
videoHolder.start();
} catch (Exception ex) {
jump();
}
所以你可以看到我使用了两次setContentView
我将创建的视图放在setContentView
给出错误的地方。我唯一需要做的就是删除这个setContentView(videoHolder);
。