简体   繁体   English

在findViewById()上出现Android NullPointerException

[英]Android NullPointerException on findViewById()

I am starting an activity and trying to get a view (in this case a VideoView) via findViewById(); 我正在开始一项活动,并尝试通过findViewById();获取视图(在本例中为VideoView findViewById(); In most cases this works just fine, but sometimes it throws a NullPointerException, when I am trying to access the view I tried to find earlier. 在大多数情况下,这工作得很好,但是当我尝试访问之前尝试查找的视图时,有时会抛出NullPointerException。

This is the Activity I am in: 这是我从事的活动:

public class Video extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        Intent intent = getIntent();
        VideoView videoView = this.findViewById(R.id.VideoViewVideo);
        videoView.setVideoURI(someURI);
        videoView.start();
    }
}

The Exception is thrown in the line videoView.setVideoURI(someURI); videoView.setVideoURI(someURI);行中引发了异常videoView.setVideoURI(someURI); and looks like this: 看起来像这样:

java.lang.RuntimeException: Unable to start activity ComponentInfo{Video}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoUri(java.lang.String)' on a null object reference
at Video.onCreate(Video.java:29)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)

The Layout file of the activity looks like this: 活动的布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="Video">

    <VideoView
        android:id="@+id/VideoViewVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

I assume there is something wrong in the way I am trying to get the view, but I don't know for sure. 我认为尝试获取视图的方式出了点问题,但我不确定。

It would be highly appreciated if someone could help me. 如果有人可以帮助我,将不胜感激。

Try to move this code 尝试移动此代码

VideoView videoView = this.findViewById(R.id.VideoViewVideo);
videoView.setVideoURI(someURI);
videoView.start();

to

@Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);

  VideoView videoView = this.findViewById(R.id.VideoViewVideo);
  videoView.setVideoURI(someURI);
  videoView.start();

}

Sometimes if you want to make a view, you have to define it in onViewCreated instead of onCreate. 有时,如果要创建视图,则必须在onViewCreated而不是onCreate中进行定义。

Let me know if it works 让我知道是否有效

Why You are writing 你为什么写作
VideoView videoView = this.findViewById(R.id.VideoViewVideo); instead of the above your can write 而不是上面你可以写

VideoView videoView = (VideoView)findViewById(R.id.VideoViewVideo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM