-3

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.

  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Michael Dodd Jul 23 '18 at 11:51
  • Are you sure `activity_main` is the xml you have posted ? I guess You probably messes up with using wrong xml file . if yes then its should work with the above code . Try clean build and re install . – ADM Jul 23 '18 at 12:23
  • Read the Exception message. The issue is in `Welcome_Screen`, not `MainActivity`, which is what you've posted. – Mike M. Jul 23 '18 at 22:50

2 Answers2

2

Maybe you are using the wrong id?

Code:

mVideoView = findViewById(R.id.videoview);

Layout:

android:id="@+id/startingvideo"

You should change one of these lines so that they are matching.

Philipp
  • 468
  • 3
  • 24
0
  1. You are using wrong ids for your mVideoView. It returns a NullPointerException because mVideoView is null, like you give wrong id. change it to :

    mVideoView = (VideoView) findViewById(R.id.startingvideo);

  2. try calling initializePlayer() from onCreate() instead of onStart();

  3. check if videoUri is not null. Your getMedia method is maybe returning a null value.

Philipp
  • 468
  • 3
  • 24
Samet
  • 917
  • 12
  • 26
  • I have corrected the ids and changed the code as you suggested but no benifit – Zubair Rasheed Jul 23 '18 at 12:15
  • Also this code runs perfectly in a seperate activity. . . but here after correctig the ids thing it is not working – Zubair Rasheed Jul 23 '18 at 12:17
  • Can you post your VideoView class please? – Samet Jul 23 '18 at 12:23
  • `try calling initializePlayer() from onCreate() instead of onStart()`? This does not make any sense . `onStart()` is called after `onCreate()`. – ADM Jul 23 '18 at 12:24
  • Yes I know, but we don't know about his VideoView class, and like he said it works on another activity and not in this one. The only thing wrong in his code was mVideoView ids. That is why I asked for his VideoView class. – Samet Jul 23 '18 at 12:40