Android VideoPlayer小部件不断提供NullPointerException,以在null对象引用上调用虚拟方法

Android VideoPlayer小部件不断提供NullPointerException,以在null对象引用上调用虚拟方法,android,android-videoview,Android,Android Videoview,现在,我正试图在我的应用程序开始播放视频作为欢迎问候,我在添加控制器之前遵循了这一点。我编写了以下代码,它在一个单独的活动中成功运行 public class MainActivity extends AppCompatActivity { private static final String VIDEO_SAMPLE = "tacoma_narrows"; private VideoView mVideoView; @Override protected void onStart() {

现在,我正试图在我的应用程序开始播放视频作为欢迎问候,我在添加控制器之前遵循了这一点。我编写了以下代码,它在一个单独的
活动中成功运行

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();
}}
public类MainActivity扩展了AppCompatActivity{
私有静态最终字符串视频\u示例=“tacoma\u缩小”;
私有视频视图;
@凌驾
受保护的void onStart(){
super.onStart();
初始化图层();
}
@凌驾
受保护的void onStop(){
super.onStop();
释放玩家();
}
@凌驾
受保护的void onPause(){
super.onPause();
if(Build.VERSION.SDK_INT
但当我试图在欢迎屏幕中添加相同的代码时,我收到了以下消息

java.lang.RuntimeException:无法启动活动 组件信息{sterlingtechsolutionpk.buttonbutton/sterlingtechsolutionpk.buttonbutton.Welcome_Screen}: java.lang.NullPointerException:尝试调用虚拟方法“void” android.widget.VideoView.setVideoURI(android.net.Uri)“”位于空 对象引用

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"/>


任何有关这方面的帮助都将不胜感激

可能您使用了错误的
id

代码:

mVideoView=findviewbyd(R.id.videoview)

布局:

android:id=“@+id/startingvideo”

您应该更改其中一行,使其匹配

  • 您的
    mVideoView
    使用了错误的ID。它返回一个
    NullPointerException
    ,因为
    mVideoView
    null
    ,就像您给出了错误的
    id
    。 将其更改为:

    mVideoView=(VideoView)findviewbyd(R.id.startingvideo)

  • 尝试从
    onCreate()
    调用
    initializePlayer()
    ,而不是
    onStart()

  • 检查
    videoUri
    是否为
    null
    。您的
    getMedia
    方法可能返回
    null


  • 是否确定
    activity\u main
    是您发布的xml?我猜您可能会因为使用了错误的xml文件而弄糟。如果是,则其应与上述代码一起工作。尝试清理生成并重新安装。请阅读异常消息。问题出现在
    Welcome\u屏幕
    ,而不是您发布的
    main活动
    。我已按照您的建议更正了ID并更改了代码,但没有任何好处。此外,此代码在单独的活动中也能完美运行。但是在这里,在纠正了ID问题之后,它不起作用了。你能发布你的VideoView类吗?
    试着从onCreate()而不是onStart()调用initializePlayer()
    ?这没有任何意义
    onStart()
    是在
    onCreate()
    之后调用的。是的,我知道,但是我们不知道他的VideoView类,就像他说的,它在另一个活动中工作,而不是在这个活动中。他的代码中唯一的错误就是mVideoView ID。这就是为什么我要上他的视频课。