现在,我正尝试在应用程序的开头播放视频,作为欢迎问候语,我在添加控制器之前遵循了此 Video player link 。我编写了以下代码,并成功在单独的Activity
中运行。
public class MainActivity extends AppCompatActivity {
private static final String VIDEO_SAMPLE = "tacoma_narrows";
private VideoView mVideoView;
@Override
protected void onStart() {
super.onStart();
initializePlayer();
}
@Override
protected void onStop() {
super.onStop();
releasePlayer();
}
@Override
protected void onPause() {
super.onPause();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mVideoView.pause();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVideoView = findViewById(R.id.startingvideo);
}
private Uri getMedia(String mediaName) {
return Uri.parse("android.resource://" + getPackageName() +
"/raw/" + mediaName);
}
private void initializePlayer() {
Uri videoUri = getMedia(VIDEO_SAMPLE);
mVideoView.setVideoURI(videoUri);
mVideoView.start();
}
private void releasePlayer() {
mVideoView.stopPlayback();
}}
但是当我尝试在欢迎屏幕中添加相同的代码时,我收到以下消息
java.lang.RuntimeException:无法启动活动 ComponentInfo {sterlingtechsolutionpk.buttonbutton / sterlingtechsolutionpk.buttonbutton.Welcome_Screen}: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.VideoView.setVideoURI(android.net.Uri)'上为null 对象引用
.xml文件是
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/loading_screen"
android:minHeight="177dp"
tools:context="sterlingtechsolutionpk.buttonbutton.Welcome_Screen">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<ProgressBar
android:id="@+id/start_progres"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="240dp"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="362dp"
android:indeterminate="false"
android:progressBackgroundTint="#fff200"
android:progressTint="#005cef"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.455" />
<VideoView
android:id="@+id/startingvideo"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="4:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
感谢您提供有关此问题的帮助。
答案 0 :(得分:2)
也许您使用的是错误的id
?
代码:
mVideoView = findViewById(R.id.videoview);
布局:
android:id="@+id/startingvideo"
您应该更改这些行之一,以便它们匹配。
答案 1 :(得分:0)
您为mVideoView
使用了错误的ID。它返回NullPointerException
,因为mVideoView
是null
,就像您输入错误的id
一样。
更改为:
mVideoView = (VideoView) findViewById(R.id.startingvideo);
尝试从initializePlayer()
而不是onCreate()
调用onStart()
;
检查videoUri
是否不是null
。您的getMedia
方法可能返回一个null
值。