Right now I am trying to play the video in the start of my App as welcome greetings I followed this Video player link before adding the controller. I wrote the following code and it ran successfully in a separate 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();
}}
but when I tried to add the same code in my welcome screen then I got the following message
java.lang.RuntimeException: Unable to start activity ComponentInfo{sterlingtechsolutionpk.buttonbutton/sterlingtechsolutionpk.buttonbutton.Welcome_Screen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(android.net.Uri)' on a null object reference
The .xml file is
<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"/>
Any Help regarding this is appreciated.